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?

3
  • What is "this"? Does it have "value"? Commented Feb 1, 2014 at 2:42
  • try consoling those "user" and "ip" variables before ajax. probably they are empty Commented Feb 1, 2014 at 2:44
  • 2
    @blurd it was the "this" which was causing the issue...thanks for spotting that Commented Feb 1, 2014 at 2:47

4 Answers 4

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:
Sign up to request clarification or add additional context in comments.

Comments

0
data: {
    page: param,
    url: url,
    ip: ip
}

Comments

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

Comments

0

Try this,

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

instead of

page = this.value

change "this" to your element for getting value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.