views:

36

answers:

3

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 ?

A: 

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));
Sarfraz
+2  A: 

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

rikh
Wouldn't it be data: { mydata: JSON.stringify(update_data) } ?
villecoder
yes, that would work as well
rikh
A: 

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

mwittrock