Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  125] [ 7]  / answers: 1 / hits: 138677  / 11 Years ago, sat, april 13, 2013, 12:00:00

Im trying to pass a mulitidimensional Javascript array to another page on my site by:




  • using JSON.stringify on the array


  • assigning the resultant value to an input field


  • posting that field to the second page


  • using json_decode on the posted value


  • then var_dump to test


  • (echo'ing the posted variable directly just to see if it came through
    at all)




Javascript on page one:



var JSONstr = JSON.stringify(fullInfoArray);
document.getElementById('JSONfullInfoArray').value= JSONstr;


php on page two:



$data = json_decode($_POST[JSONfullInfoArray]);
var_dump($data);

echo($_POST[JSONfullInfoArray]);


The echo works fine but the var_dump returns NULL



What have I done wrong?






This got me fixed up:



$postedData = $_POST[JSONfullInfoArray];
$tempData = str_replace(\, ,$postedData);
$cleanData = json_decode($tempData);
var_dump($cleanData);


Im not sure why but the post was coming through with a bunch of characters separating each variable in the string



Figured it out using json_last_error() as sugested by Bart which returned JSON_ERROR_SYNTAX


More From » php

 Answers
30

You'll need to check the contents of $_POST[JSONfullInfoArray]. If something doesn't parse json_decode will just return null. This isn't very helpful so when null is returned you should check json_last_error() to get more info on what went wrong.


[#78937] Thursday, April 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tierra

Total Points: 705
Total Questions: 85
Total Answers: 96

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;