1

I want to send the result of a HTML sorting to the server by serializing with jQuery. This works if I only send the result:

var result = $(this).sortable('serialize');

$.ajax({
    type: 'POST',
    url: '/cms/update/',
    data: result,
});

But I try to send a Javascript Object to the server wich contains the serialized 'result'

In PHP I get an array with result_2 as the serialize object:

Array
(
    [ids_1] => miti_1_ti_2_col_2
    [article_id] => article_id_2
    [result_1] => 
    [ids_2] => miti_1_ti_2_col_1
    [result_2] => article_id[]=2
)

How can I get this result to be an array in PHP?

3
  • json good enough? php have json_decode/encode. Commented Dec 15, 2011 at 21:33
  • The easiest way would be to send your POST data in array format in the first place. In other words ids[1]=miti_1_ti_2_col_2&ids[2]=miti_1_ti_2_col_1&... etc. Commented Dec 15, 2011 at 21:37
  • I think this indeed is the easiest way to go, thanks Commented Dec 16, 2011 at 13:57

2 Answers 2

2

As I understood "result" is a serialized object too.

So you have to unserialize result at first.

Then you have to unserialize result2. Something like that:

$res1 = unserialize($data);
if (isset($res1['result_2']){
   $res2 = unserialize($res['result_2']);
}

Updated:

I don't know if your result_2 in data is already serialized. Therefore here are two examples:

if result_2 is not serialized in data:

$arr = array('id_1' => 'miti_1_ti_2_col_2',
             'article_id' => 'article_id_2',
             'result_1' => '',
             'ids_2' => 'miti_1_ti_2_col_1'
            );

$arr['result_2'] = $arr;

$test1 = serialize($arr);
$test1 = unserialize($test1);

If result_2 is already serialized in data:

$arr = array('id_1' => 'miti_1_ti_2_col_2',
             'article_id' => 'article_id_2',
             'result_1' => '',
             'ids_2' => 'miti_1_ti_2_col_1'
            );

$arr['result_2'] = serialize($arr);

$test2 = serialize($arr);
$test2 = unserialize($test2);

$test2['result_2'] = unserialize($test2['result_2']);

This code works I checked out it. If your code still doesn't work check result in JS.

Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't seem to work: $res1 = unserialize($_POST); if (isset($res1['result_2'])){ $res2 = unserialize($res1['result_2']); } print_r($res2);
The post is in the Question it's the array
1

If I've understood correctly, you need to convert a string such as action[]=1&action[]=2 into an array?

If that is right you can use the following: (when $_POST["order"] = "action[]=1&action[]=2")

$result = preg_split("/&?action\[\]=/", $_POST["order"], -1, PREG_SPLIT_NO_EMPTY);

This will give you:

Array
(
    [0] => 1
    [1] => 2
)

1 Comment

To work with article_id use: preg_split("/&?article\_id\[\]=/", $_POST["result_2"], -1, PREG_SPLIT_NO_EMPTY)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.