Take the 2-minute tour ×

Using $('#form').serialize(), I was able to send this over to a PHP page. Now how do I unserialize it in PHP? It was serialized in jQuery.

share|improve this question
For questions like these I recommend using Firebug to see what data in what format is sent to the server. You can then easily post an example of your data. –  chiborg Jun 3 '10 at 9:57
add comment

9 Answers

up vote 15 down vote accepted

You shouldn't have to unserialize anything in PHP from the jquery serialize method. If you serialize the data, it should be sent to PHP as query parameters if you are using a GET method ajax request or post vars if you are using a POST ajax request. So in PHP, you would access values like $_POST["varname"] or $_GET["varname"] depending on the request type.

The serialize method just takes the form elements and puts them in string form. "varname=val&var2=val2"

share|improve this answer
Thank you Chris !! –  Gene Bean Nov 24 '09 at 20:19
See, I told you php was awesome. –  Edward Sep 30 '13 at 20:43
"was" wink ;) –  JohnnyQ Nov 29 '13 at 6:21
add comment

Provided that your server is receiving a string that looks something like this (which it should if you're using jQuery serialize()`):


...something like this is probably all you need:

$params = array();
parse_str($_GET, $params);

$params should then be an array modeled how you would expect. Note this works also with HTML arrays.

See the following for more information: http://www.php.net/manual/en/function.parse-str.php

Hope that's helpful. Good luck!

share|improve this answer
That's the best way of doing it. parse_str($_REQUEST['jquery_serialized_values'], $output); print_r($output); –  Andy Dec 28 '10 at 17:06
This answer should be accepted, the actual one is no answer. –  Ismael Nov 14 '11 at 12:42
This method is MUCH better than the other. –  dartacus Nov 18 '11 at 11:13
This is the correct answer, thanks chrisallenlane. –  Siva Dec 2 '11 at 12:13
As the user that gave the accepted answer, I agree that THIS is the correct answer. I gave my answer based on the assumption that the request was being sent via the jQuery ajax $.get/post methods (not sure where I got that from) which does send the output of the serialize() function as standard GET variables. so no string parsing would be required. eg: $.get('ajax.php', $('form').serialize(), function() {}); –  Chris Gutierrez Sep 12 '12 at 17:36
show 2 more comments

// jQuery Post

var arraydata = $('.selector').serialize();

// jquery.post serialized var - TO - PHP Array format

parse_str($_POST[arraydata], $searcharray);
print_r($searcharray); // Only for print array

// You get any same of that

 Array (
 [A] => 1
 [B] => 2
 [C] => 3
 [D] => 4
 [E] => 5
 [F] => 6
 [G] => 7
 [H] => 8
share|improve this answer
That was exactly what I was searching for! –  jepser Aug 3 '12 at 1:08
add comment
parse_str($_POST['whatever'], $searcharray);
share|improve this answer
add comment

I don't know which version of Jquery you are using, but this works for me in jquery 1.3:

    type: 'POST', 
    url: your url,
    data: $('#'+form_id).serialize(), 
    success: function(data) {

Then you can access POST array keys as you would normally do in php. Just try with a print_r().

I think you're wrapping serialized form value in an object's property, which is useless as far as i know.

Hope this helps!

share|improve this answer
add comment

This is in reply to user1256561. Thanks for your idea.. however i have not taken care of the url decode stuff mentioned in step3.

so here is the php code that will decode the serialized form data, if anyone else needs it. By the way, use this code at your own discretion.

function xyz($strfromAjaxPOST)
    $array = "";
    $returndata = "";
    $strArray = explode("&", $strfromPOST);
    $i = 0;
    foreach ($strArray as $str)
        $array = explode("=", $str);
        $returndata[$i] = $array[0];
        $i = $i + 1;
        $returndata[$i] = $array[1];
        $i = $i + 1;

The url post data input will be like: attribute1=value1&attribute2=value2&attribute3=value3 and so on

Output of above code will still be in an array and you can modify it to get it assigned to any variable you want and it depends on how you want to use this data further.

    [0] => attribute1
    [1] => value1
    [2] => attribute2
    [3] => value2
    [4] => attribute3
    [5] => value3
share|improve this answer
add comment

I think you need to separate the form names from its values, one method to do this is to explode (&) so that you will get attribute=value,attribute2=value.

My point here is that you will convert the serialized jQuery string into arrays in PHP.

Here is the steps that you should follow to be more specific.

  1. Passed on the serialized jQuery to a PHP page(e.g ajax.php) where you use $.ajax to submit using post or get.
  2. From your php page, explode the (&) thus separating each attributes. Now you will get attribute1=value, attribute2=value, now you will get a php array variable. e.g$data = array("attribute1=value","attribute2=value")
  3. Do a foreach on $data and explode the (=) so that you can separate the attribute from the value, and be sure to urldecode the value so that it will convert serialized values to the value that you need, and insert the attribute and its value to a new array variable, which stores the attribute and the value of the serialized string.

Let me know if you need more clarifications.

share|improve this answer
add comment

Why don't use associative array, so you can use it easily

function unserializeForm($str) {
    $returndata = array();
    $strArray = explode("&", $str);
    $i = 0;
    foreach ($strArray as $item) {
        $array = explode("=", $item);
        $returndata[$array[0]] = $array[1];

    return $returndata;


share|improve this answer
add comment

just uft8_decode($_POST['message']);

share|improve this answer
add comment

Your Answer


