ctarimli ctarimli - 6 months ago 15
PHP Question

$_POST gives error in Internet Explorer

I make an ajax call for checking the availability of the username or for the validation of the passwords etc..

the ajax part :

$('#usernameinput').on('keyup change', function() {
$.ajax({
type:'POST',
url:"formchk.php",
data: $("#registform").serialize(),
dataType:"html",
success: function(result){
$('#span').html(result)
}} )});


php part :

include("connect.php");
$username = mysql_real_escape_string($_POST['username']);
$stm = $bgln->query("SELECT * FROM membrs where username='$username'");
$row_cntusername = $stm->num_rows;
if( strlen(mysql_real_escape_string($_POST['username'])) > 20) {
echo "<a id=\"error\">something something </a>";
$stm->close();
}
elseif($row_cntusername > 0){
echo "<a id=\"error\">In use</a>";
$stm->close(); }
//and other checkings goes on...


I searched a lot but can't figure out, what is the problem here. This works fine in chrome and FF but not in IE.

Note:


  • jquery library is jquery-2.1.0.min.js.

  • IE version is 11.



Edit:
I tried to use
$_GET
instead of using
$_POST
. It worked! But this is not the solution, I am using these codes for inserting and updating data, too. And I believe it'll be better if i use $_POST when inserting and/or updating data to database.

Edit2

I added this in php part ;

if (count($_POST) == 0) {
die('No posted data received');
}


I get the result of the call as 'No posted data recieved'

And I tried in javascript part;

document.write($("#registform").serialize())


It gives the value that wrote in input. So, the problem is ajax can't posts the data to the php file but gets a result from php as 'No posted data'.

I also added

cache: false,


if its a cache problem of the IE, but it didn't work, too.

Answer

You need more validation on your POST data:

include("connect.php");

if (count($_POST) == 0) {
    die('No posted data received');
}

$fields = array('username', 'kadi', ...);
foreach ($fields as $field) {
    if (!isset($_POST($field)) {
        die('Missing required parameter: ' . $field);
    }
}

$username = mysql_real_escape_string($_POST['username']);
$stm = $bgln->query("SELECT * FROM membrs where username='$username'");
    $row_cntusername = $stm->num_rows;
    if( strlen(mysql_real_escape_string($_POST['kadi'])) > 20) {
        echo "<a id=\"error\">something something </a>";
            $stm->close();
        }
    elseif($row_cntusername > 0){
            echo "<a id=\"error\">In use</a>";
            $stm->close(); }
//and other checkings goes on...

Change your jQuery to use a simple object instead of a serialized string. Set a variable beforehand for each input field:

$('#usernameinput').on('keyup change', function() {
   var name = $('#name').val();
   // ...
   $.ajax({
       type:'POST',
       url:"formchk.php",
       data: { name : name },
       dataType:"html",
       success: function(result){
         $('#span').html(result) 
       }}
)});