0

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.

ctarimli
  • 322
  • 4
  • 25

2 Answers2

0

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) 
       }}
)});
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
  • Thank you. I tried it. now it gives "No posted data recieved" error as you write in code. But I dont understand, why it doesn't posts the data. :( – ctarimli Feb 20 '14 at 17:37
  • I added an example to try with your jQuery. – NobleUplift Feb 20 '14 at 22:57
  • I tried your example. Still works in other browsers but not in IE. – ctarimli Feb 20 '14 at 23:06
  • 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. – ctarimli Feb 21 '14 at 00:42
  • Yes, you are supposed to use `POST` for calls that might not be idempotent. Try using [jQuery.post()](https://api.jquery.com/jQuery.post/) instead of `jQuery.ajax()`. – NobleUplift Feb 21 '14 at 15:28
  • Tried it but doesn't help :( – ctarimli Feb 21 '14 at 21:52
  • Did you try sending in the data as a JavaScript object instead of a string? Try reducing the form to only **one** field and then send it in. Add fields back in if you start getting data. – NobleUplift Feb 22 '14 at 23:49
  • Serialize must have been creating a bad query string, or no string at all. – NobleUplift Feb 23 '14 at 22:33
0

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',
  ...