Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
319 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.

json_decode( html_entity_decode( stripslashes ($jsonString ) ) );

Thanks to @Thisguyhastwothumbs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...