61

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

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • 3
    Check the content of `$_POST["JSONfullInfoArray"]`. If something doesn't parse `json_code` will just return `null`. Also use `json_last_error()` to try and find what went wrong. – Bart Apr 13 '13 at 09:25
  • 1
    A little bit HTML of the form would help too. Look into the request using your browser's inspector, do you see what you expect? – UltraInstinct Apr 13 '13 at 09:33
  • @Bart see edit above, you got me where I needed to be, throw up an answer and Ill accept it :), thank you very much – Wesley Smith Apr 13 '13 at 10:05
  • 3
    The answer in here worked for me (and all the other 'answers' below didn't!). – Chris Rae Apr 02 '15 at 22:50

7 Answers7

61

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

S. A. Malik
  • 3,465
  • 6
  • 37
  • 56
  • 1
    Ok, this is like the right answer and what I was looking for. Saved me a couple of hours of disgusting trial and error. – MarkSkayff Jan 24 '19 at 19:32
  • Thanks this is work for me!!! ... In $request["reporthead"] data as string which is from javascript $tempData = \html_entity_decode(stripslashes ($request["reporthead"])); $reportheaddata = json_decode($tempData, true); – Priyanka Ahire Dec 23 '20 at 12:30
34

When you use JSON stringify then use html_entity_decode first before json_decode.

$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);
user1881928
  • 606
  • 6
  • 8
  • 3
    worth noting in wp I used the above with stripslashes within the html_entity_decode to make work : – treyBake Jan 22 '18 at 14:27
27

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.

Bart
  • 17,070
  • 5
  • 61
  • 80
11

None of the other answers worked in my case, most likely because the JSON array contained special characters. What fixed it for me:

Javascript (added encodeURIComponent)

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

PHP (unchanged from the question)

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

echo($_POST["JSONfullInfoArray"]);

Both echo and var_dump have been verified to work fine on a sample of more than 2000 user-entered datasets that included a URL field and a long text field, and that were returning NULL on var_dump for a subset that included URLs with the characters ?&#.

Yuv
  • 314
  • 3
  • 8
  • 2
    I also had to use `urldecode` on PHP for it to work properly. – Albion S. Mar 13 '17 at 08:37
  • 4
    As @Albion points out, the string needs to be URI decoded before `json_decode` is invoked. `json_decode(urldecode($_POST["JSONfullInfoArray"]));` – Aurovrata Jul 31 '17 at 13:11
8
stripslashes(htmlspecialchars(JSON_DATA))
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
LOVEPIN
  • 89
  • 1
  • 2
  • 3
    I am not sure why `htmlspecialchars()` must there. It will convert the quote character ( `"` ) to `"` which is mean can't be processed by `json_decode()`. I am sure `JSON` format must be something like `{"key":"value"}`, but by using `htmlspecialchars()` it will convert to `{"key":"value"}` how come `json_decode()` processing it? – Bayu Oct 04 '16 at 06:53
3
jsonText = $_REQUEST['myJSON'];

$decodedText = html_entity_decode($jsonText);

$myArray = json_decode($decodedText, true);`
Jobin
  • 5,610
  • 5
  • 38
  • 53
0

I don't how this works, but it worked.

$post_data = json_decode(json_encode($_POST['request_key']));
user10971804
  • 253
  • 2
  • 8