1

I'm using this jQuery AJAX function and I'm trying to figure out how to use the 'data:' part of it. According to this page (http://api.jquery.com/jQuery.ajax/) I can use 'data:' to send the number 22 to 'process_stage.php' so I can use it.

Can anyone tell me what I need to type in my process_stage.php page to access the number 22?

function myAJAX(){
$.ajax({                                      
url: 'process_stage.php',     
      data: '22',    
      dataType: 'json',                             
      success: function(data) {             
        var videoid = data[0];      
        var currentID = data[1];
        $('#youtube').html("<iframe width='400' height='225' src='http://www.youtube.com/embed/"+videoid+"?rel=0&amp;autohide=1&amp;showinfo=0&amp;autoplay=1' frameborder='0' allowfullscreen></iframe>");
        setTimeout(function (){
            timedCount(currentID);
            },1000);
        }
});
}
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
aadu
  • 3,196
  • 9
  • 39
  • 62

2 Answers2

6

As you're making a HTTP GET request, data needs to be key-value pairs, as that's how a GET request is constructed (e.g. /get.php?var1=a&var2=b&var3=c).

jQuery.ajax() accepts this key-value pairs as either an object map, or a string, as described in the documentation:

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

So you should use either;

data: "value=22"

or

data: {
    value: 22
}

Then in PHP you can use $_GET['value'] to retrieve it.

Matt
  • 74,352
  • 26
  • 153
  • 180
  • Okay, thanks, so if I write it like that, do I use $_GET['value'] to access it on my PHP page? – aadu Jul 06 '12 at 13:38
  • 2
    Bah, I refresh the page after typing my answer only to discover someone has already answered :) – tomds Jul 06 '12 at 13:41
2

Your value needs a field name to go with it. To do this, make data an object e.g. {my_value: 22}. Then in your PHP script look for the field called my_value.

tomds
  • 444
  • 3
  • 13