Seyyid Said
Seyyid Said

Reputation: 525

jquery ajax how to send whole number integers to php as variables

Please how can I send a whole number like twelve to php using ajax. I have been able to send string variables using both GET and POST methods successfully, but when it comes to numerical values it becomes a problem , I don't know why.below is my jQuery

 function user_ajax_call(){
    var data = $(".people_names").length;    
    var more_loader = $("<img id='hiddenL' src='../ForePost/icons/spin.gif'/>");
    $("#pple").append(more_loader);
    $.ajax({
        url: 'http://localhost/Forepost/mod/loadmore_data.php',
        dataType: 'text',
        type: 'POST',
        data:{data:data},
        processData: false,
        contentType: false,
        cache:false,
        success: function(returndata){
            $("#pple").append(returndata);
            more_loader.hide();
        },
        error: function () {

        }
    });
}

And these are sample php lines

$limistart = $_POST['data'];

    if(isset($limistart)){
        echo $limistart;
    }

Upvotes: 1

Views: 4087

Answers (2)

C&#233;sar Ferreira
C&#233;sar Ferreira

Reputation: 681

You need to send them through: data. You could do something like this in your data variable:

data = {
 name_length : $(".people_names").length,
 number : 12
};

And just pass it like this in your ajax:

 function user_ajax_call(){
     var data = {
         name_length : $(".people_names").length,
         number : 12 
     };
     var more_loader = $("<img id='hiddenL' src='../ForePost/icons/spin.gif'/>");

    $("#pple").append(more_loader);
    $.ajax({
        url: 'http://localhost/Forepost/mod/loadmore_data.php',
        dataType: 'text',
        type: 'POST',
        data: data,
        success: function(returndata){
            $("#pple").append(returndata);
            more_loader.hide();
        },
        error: function () {

        }
    });
}

And in your server side access it like :

$_POST['name_length']
$_POST['number']

Upvotes: 1

shlomi
shlomi

Reputation: 3

If you change the value of contentType key it should work correctly.

So change this:

contentType: false

to:

contentType: "application/x-www-form-urlencoded; charset=UTF-8"

EDIT:

and change the line:

processData: false

to:

processData: true

Upvotes: 0

Related Questions