简体   繁体   中英

$_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.

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) 
       }}
)});

I know this is an old question but I just had the same problem, empty $_POST array, and found another solution after re-reading the jQuery docs. If I change the ajax param: "type: 'post'" to "method: 'post'" it all works in IE as well as the other browsers... in the PHP code the $_POST array is correctly populated.

$.ajax(
  {
  method: 'post',
  dataType: 'json',
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM