简体   繁体   中英

How do I unserialize a jQuery-serialize value in php

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

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.

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);

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM