phpjqueryjquery-nestable

php json_decode and Jquery Nestable


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..


Solution

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

    $data = json_decode($data, true);