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?

2
  • 1
    I think there was a function serialize(), which converts the data. Commented Feb 11, 2011 at 12:52
  • Very similar to the question posed here: stackoverflow.com/questions/191881/… Commented Feb 11, 2011 at 13:34

1 Answer 1

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.

1
  • 1
    Cool Andy. This, in association with $menuArray = json_decode($_POST['menuArray'],true); has done the trick ;) Commented Feb 11, 2011 at 13:10

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.