Javascript json array to php array
I was trying to use jQuery UI Nested Sortable 1.2.1 from http://mjsarfatti.com/sandbox/nestedSortable where it prepares a javascript array as
ret.push({"id": id, "par_id": pid, "title": title, "depth": depth, "left": left, "right": right});
I have tried to send that data through a hidden field as
<input id="menuArray" name="menuArray" type="hidden" value="" />
$('#submit').click(function(){
var ma = $('ol.sortable').nestedSortable('t开发者_开发知识库oArray');
$("#menuArray").val(ma);
$('form#target').submit();
});
However, when I do
echo '<pre>';
print_r($_POST['menuArray']);
echo '</pre>';
All I get is:
<pre>[object Object],[object Object],[object Object] ...</pre>
Any solution / tips / hints friends?
You're not actually JSON encoding anything anywhere. You need to use JSON.stringify
when you set the value of the hidden input:
$('#submit').click(function() {
var ma = $('ol.sortable').nestedSortable('toArray');
$("#menuArray").val(JSON.stringify(ma));
$('form#target').submit();
});
Note that JSON.stringify isn't supported by older browsers (like IE 7), you'll have to include json2.js for full support.
精彩评论