Tutorial :How to collect jquery.ajax() data in PHP?



Question:

I send data from javascript to PHP like this:

$.ajax({'url': 'my.php',          'type': 'POST',          'data': JSON.stringify(update_data),          'success': function(response) {                         alert(response);                     }         });  

Using HTTPFOX Firefox plug-in I see the following data in the POST DATA tab:

{"file_id":["1","2","3"],"description":["lala","kuku","wow!"],"tags":[["julia","paper"],["Very nice car"],[]]}

however, if I do in my.php print_r($_POST) I see an empty array. Why is that ? How could I collect the data ?


Solution:1

The data needs to be in the form name=value.

try...

$.ajax({'url': 'my.php',          'type': 'POST',          'data': 'mydata=' + JSON.stringify(update_data),          'success': function(response) {                         alert(response);                     }         });  

Then you should have your json string in $_POST['mydata']

You'll then need to use json_decode to actually get at the individual values in your string.

http://php.net/manual/en/function.json-decode.php


Solution:2

The response you are getting is in JSON format. Use the json_decode function to convert it to array.

print_r(json_decode($_POST['description'], true));  


Solution:3

I'd expect your data to be in $HTTP_RAW_POST_DATA. Then json_decode to make sense of it.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »