Hello I have problem with deserialization of JSON serialized in javascript. I have form on webpage and every row is one product (inputs, selects, checkboxes in form):
name, price, total_count, ...
name2, price2, total_count2, ...
...
I take a form and serialize it with javascript (function ajaxLoad is normal shortened jQuery fuction $.ajax(...) and it works at another places correctly )
var form = $('#myForm');
form.submit(function(event){
event.preventDefault();
ajaxLoad(
form.attr('action'),
form.parent(),
{jsonData : JSON.stringify(form.serializeArray())}
);
});
in php, the data are received and my code is following:
$data = json_decode($jsonData, true);
$this->template->data = print_r($data,1);
it returns something like that:
Array
(
[0] => Array
(
[name] => products[0][cor_projectProduct_name]
[value] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
)
[1] => Array
(
[name] => products[0][url]
[value] => http://some-nice-url.cz
)
[2] => Array
(
[name] => products[0][cor_projectProduct_ean]
[value] =>
)
[3] => Array
(
[name] => products[0][cor_projectProduct_internalCode]
[value] =>
)
[4] => Array
(
[name] => products[0][cor_project_id]
[value] => 6
)
[5] => Array
(
[name] => products[0][cor_projectProduct_keywordAllowed]
[value] =>
)
...
but I would like to have array of objects.
I've tried to serialize the form with form.serialize
as well, but returned result was even worse - urlencoded string, that I couldn't decode.
When I've tried to send data via POST method as array and then read it in php from $_POST
, it worked but some data was lost due to POST limitation, so it is better to post it as serialized string, but I don't know how and how to deserialize it in php.
EDIT: OK, maybe it was badly explained, what I need as a result is array of Objects:
Array
(
[0] => stdClass Object
(
[cor_projectProduct_name] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
[url] => http://www.ceske-mince.cz/ceska_mincovna/1997/replika-kadnerova-jachymovskeho-tolaru-stand/
[cor_projectProduct_ean] =>
[cor_projectProduct_internalCode] =>
[cor_project_id] => 6
[cor_projectProduct_keywordAllowed] =>
...
)
[1] => stdClass Object
(
[cor_projectProduct_name] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
...
)
...
or the same with associative arrays, it does not matter