1

I'm trying to use AJAX to POST to a page and display the results but it's not sending all of the parameters.

function change_page(button){
    var parent_div = $(button.parentNode);
    var param = parent_div.attr("id");
    var page = this.value;
    var user = $("#user").val();
    var ip = $("#ip").val();
    $.ajax({
        url: "gui_info.php",
        type: 'POST',
        data: { param : page, "user" : user, "ip" : ip },
        dataType: "html",
        success: function(data){
            parent_div.html(data);
            alert(data);
        }
    }); 
}

On the php page it's sending to, I'm using print_r($_POST) to see which parameters are actually received. Only user and ip are succesfully being sent since the response is Array ( [user] => [ip] => ) whereas the param key/value aren't. I've checked the value of the the variables not being sent, they both exist. What am I doing wrong?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
user2923125
  • 77
  • 1
  • 7

4 Answers4

6

You are not seeing the variable in the request because its value is undefined. Undefined values are not included in the jQuery POST.

For example, when sending data like this...

$.ajax({
  type: 'POST',
  data: {
    a: 1,
    b: undefined,
    c: false,
    d: null
  }
});

...these are the parameters passed in the XHR request:

a: 1
c: false
d:
reergymerej
  • 2,371
  • 2
  • 26
  • 32
0
data: {
    page: param,
    url: url,
    ip: ip
}
0

All you have to do is this :

$.ajax({
    url: "gui_info.php",
    type: 'POST',
    data: { 
      param : page, 
      user : user, 
      ip : ip 
     },
    dataType: "json",   // pass json in there
    success: function(data){
        parent_div.html(data);
        alert(data);
    }
 }); 
Makrand
  • 587
  • 5
  • 14
0

Try this,

page = $(button).val() //If "button" is an element

instead of

page = this.value

change "this" to your element for getting value

Snehal S
  • 865
  • 4
  • 13