2

I'm using jquery Nestable (http://dbushell.github.io/Nestable/).

I'm trying to intercept data from it when I change order on mine nestable elements..

$('.dd').nestable().on('change', function() {
  var json_text = $('.dd').nestable('serialize');
  $.post("/includes/processes/core.php", {
    cmd: 'nestable',
    table: table,
    data: json_text
  }, function(response){
    alert(response);
  });
});

In core.php I've done:

$data = $_POST['data'];
$data = json_decode($data, true);
function parseJsonArray($jsonArray, $parentID = 0) {
  $return = array();
  foreach ($jsonArray as $subArray) {
    $returnSubSubArray = array();
    if (isset($subArray['children'])) {
  $returnSubSubArray = parseJsonArray($subArray['children'], $subArray['id']);
    }
    $return[] = array('id' => $subArray['id'], 'parentID' => $parentID);
    $return = array_merge($return, $returnSubSubArray);
  }
  return $return;
}

$readbleArray = parseJsonArray($data);

Than I've to manipulate this array but I can't go on because response is:

"json_decode() expects parameter 1 to be string, array given in core.php on line (where is "$data = json_decode($data, true;")"

If I change json_decode($data, true) with json_encode($data) this is the response:

"Invalid argument supplied for foreach() in core.php on line (where is "foreach ($jsonArray as $subArray);")"

Please Help me..

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Sergio Pisoni
  • 107
  • 3
  • 12

2 Answers2

1

Use JSON.stringify. It will turn your javascript object into a JSON string, which will allow you to post it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

You can than decode it in PHP. To do this, in your jQuery code replace this:

data: json_text

With:

data: {json_text: JSON.stringify(json_text)}
anthonygore
  • 4,722
  • 4
  • 31
  • 30
1

Try removing this line because $.post doesn't do json encoding.

$data = json_decode($data, true);
vooD
  • 2,881
  • 2
  • 25
  • 34