I am trying this new method I've seen serializeArray()
.
//with ajax
var data = $("#form :input").serializeArray();
post_var = {'action': 'process', 'data': data };
$.ajax({.....etc
So I get these key value pairs, but how do I access them with PHP?
I thought I needed to do this, but it won't work:
// in PHP script
$data = json_decode($_POST['data'], true);
var_dump($data);// will return NULL?
Thanks, Richard
its possible by using the serialize array and json_decode()
the values are received as an array
each value can be retrieved using $value[0]['value'] each html component name is given as $value[0]['name']
the javascript doesn't change the way that the values get posted does it? Shouldn't you be able to access the values via PHP as usual through
$_POST['name_of_input_goes_here']
edit: you could always dump the contents of $_POST to see what you're receiving from the javascript form submission using
print_r($_POST)
. That would give you some idea about what you'd need to do in PHP to access the data you need.The JSON structure returned is not a string. You must use a plugin or third-party library to "stringify" it. See this for more info:
http://www.tutorialspoint.com/jquery/ajax-serializearray.htm
The OP could have actually still used serializeArray() instead of just serialize() by making the following changes:
I have a very similar situation to this and I believe that Ty W has the correct answer. I'll include an example of my code, just in case there are enough differences to change the result, but it seems as though you can just use the posted values as you normally would in php.
This is a really simplified example, but I think the key point is that you don't want to use the
json_decode()
method as it probably produces unwanted output.You can use this function in php to reverse serializeArray().