2

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('toArray');
$("#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?

Jeremy Roy
  • 1,291
  • 5
  • 18
  • 31

1 Answers1

2

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.

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 1
    Cool Andy. This, in association with `$menuArray = json_decode($_POST['menuArray'],true);` has done the trick ;) – Jeremy Roy Feb 11 '11 at 13:10