Reputation: 61
I am able to send jquery serialize value in php file. But I can't unserialize jquery serialize value in php. Now Please, tell me How do I unserialize jquery serialize value in php file. my jquery code is :
<script type="text/javascript">
// index.php
function get(){
$('#age').hide();
$.post('data.php',{ name: $('form').serialize() },
function (output){
$('#age').html(output).fadeIn(1000);
});
}
</script>
data.php file is:
<?php
echo var_dump(unserialize($_POST['name']));
?>
I use above code for unserialize jquery serialize value but I don't get any result
Upvotes: 0
Views: 9763
Reputation: 41
I had the same issue. The serialized array is an array of name:xxx, value:....
Here's what I did to convert it to a PHP object.
$data=json_decode(file_get_contents("php://input"));
$obj=new \StdClass;
foreach ($data as $row){
$key=$row->name;
$value=$row->value;
$obj->$key=$value;
}
So, here, it is iterating through the JQuery serialized array, and creating a PHP object property with the "name" value, and giving the property a value with the "value" value.
Upvotes: 0
Reputation: 6296
In general, you should not have to unserialise the data.
If the data is being passed as the for parameters, then the following will suffice.
$form_values = $_POST;
print_r($form_values);
However, sometimes you might want to pass serialised string as one of the fields of the form. In this case, you can use something like the following:
$form_values = $_POST;
$serialised_form_value = $form_values['some_field_from_form'];
parse_str($serialised_form_value, $field_value);
print_r($field_value);
Upvotes: 2
Reputation: 154
sorry reposted my answer 100% work for my work correctly jquery =>
var data = $('form').serialize();
$.post(baseUrl+'/ajax.php',
{action:'saveData',data:data},
function( data ) {
alert(data);
});
php =>
parse_str($_POST['data'], $searcharray);
echo ('<PRE>');print_r($searcharray);echo ('</PRE>');
output =>
[query] =>
[search_type] => 0
[motive_note] =>
[id_state] => 1
[sel_status] => Array
(
[8] => 0
[7] => 1
)
and You can do whatever what you want like with "$searcharray" variable data, thanks to Aridane https://stackoverflow.com/questions/1792603
Upvotes: 0
Reputation: 34107
You need not unserialize anything. The function's name is a bit misleading. jQuery's .serialize()
creates a string representation of the form just like as if it was sent by a regular form, for example, calling .serialize()
on the following form:
<input name="a" value="foo" />
<input name="b" value="bar" />
will result in this string:
a=foo&b=bar
If you modify your ajax call to:
$.post('data.php', $('form').serialize(), function (output) {...});
Your form will simply be in $_POST
, ready to use.
Upvotes: 1
Reputation: 101
How about simplify it to:
<script type="text/javascript">
// index.php
function get(){
$('#age').hide();
$.post('data.php',$('form').serialize(),
function (output){
$('#age').html(output).fadeIn(1000);
});
}
</script>
and in data.php
<?php
$name=$_POST;
echo var_dump($name);
?>
Using serialize here is same as a form submit.
Upvotes: 4
Reputation: 7853
Well, you are sending a JSON object to your PHP script so you should use json_decode()
Ultimately you are not getting any results because the $_POST
data is interpreted as a string, granted a string with JSON syntax but still nothing but a simple string to PHP. To get the contents you must use the json_decode()
function linked to above. By default it will return a stdClass
object, pass true
to the second parameter to return an associative array
// return stdClass or null on error
$jsonData = json_decode($_POST['name']);
// return associative array or null on error
$jsonData = json_decode($_POST['name'], true);
You should make sure to check if there is a value stored in $_POST['name']
before using it.
Upvotes: 1