0

I'm sending an entire form, about 8 fields, along with my AJAX data object, one of which is a serialized string:

var fields = $(this).serialize();

var data = { 
    action:'newSubmission',
    nonce: Nonce,
    fields: fields
}; 

Now within PHP I cannot understand how to get each field value from the $_POST object correctly.

$test = $_POST['field'];

Is the full string and sends back to JS a properly formatted JSON object. But how do I break up the serialized string correctly in PHP?

Ben Racicot
  • 5,332
  • 12
  • 66
  • 130

2 Answers2

1

The string will be encoded, so you will have to do that with url_decode. Here is a function that I used to build a workable object out of the string.

    function urldecode_to_obj($str) {

        $str = trim($str, '"');

        // explode string before decoding
        foreach (explode('&', $str) as $chunk) {
            $param = explode("=", $chunk);

            if ($param) {

                // search string for array elements and look for key-name if exists
                preg_match('#\[(.+?)\]$#', urldecode($param[0]), $with_key);
                preg_match('#\[\]$#', urldecode($param[0]), $no_key);

                $mkey = preg_split('/\[/', urldecode($param[0]));

                // converts to array elements with numeric key
                if ($no_key) {
                    $data[$mkey[0]][] = urldecode($param[1]);
                }

                // converts to array elements with named key
                if ($with_key) {
                    $data[$mkey[0]][$with_key[1]] = urldecode($param[1]);
                }

                if (!$no_key && !$with_key) {
                    $data[urldecode($param[0])] = urldecode($param[1]);
                }

            }

        }

        return (object)$data;
    }

$str = "serialized_string";
$obj = urldecode_to_obj($str);

Your variables with values are now in the object. If you have a var1 variable in there with a value1:

$obj -> var1 = "value1";

You can also get thins back as an array. Just omit the (object) casting and return the data in t\he function as:

return $data;

If you want to return things to your JS then you should echo them out as an array and use json_encode:

$array = array("success" => true);
echo json_encode($array);
Daniel
  • 4,816
  • 3
  • 27
  • 31
  • Its really this complicated eh? I used your function and returned its data to my JS. I got "full": {}, – Ben Racicot Jan 31 '15 at 03:20
  • I am not entirely sure what you wish to do. Why do you want to go right back to JS. You get your string to the back-end and want something you can work with. This will do it. I added some to the code to show how to use the function. Also how to tell your JS about success. Hope that will help you. – Daniel Jan 31 '15 at 05:37
  • Hey Dan, I'm sending it back to JS to make sure I've got the data right. That's it. I'll keep working on your example code. Thanks btw. – Ben Racicot Jan 31 '15 at 13:40
-1

Try using:

$serializedData = file_get_contents("php://input");
$unserializedData = array();
parse_str($unserializedData,$serializedData);
print_r($unserializedData);

instead of the PHP $_POST superglobal use the php://input.

PHP "php://input" vs $_POST

Then you can use theparse_str() php function to turn the serialized string into a php array.

There are also a number of other ways to manually do this if you so choose:

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

return $returndata;
}
Community
  • 1
  • 1
parallaxis
  • 196
  • 1
  • 13
  • wtf? why the down vote? is it this that you are looking for http://php.net/manual/en/function.json-decode.php – parallaxis Jan 31 '15 at 02:17