1

so i have a gigantic javascript object that i want to pass it on to PHP.

What i'm trying is: - use stringify to put it as the value of a hidden field - hit submit

In PHP, if I

echo $_POST['hidden'] 

the JSON string seems perfect, but when i use

json_decode($_POST['hidden']) 

i get NULL

If i use jQuery's

$.post

, i get exactly the desired result: i am able to use json_decode on it.

Can someone explain to me what i'm doing wrong? thanks

André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120

3 Answers3

1

Your JSON string might contain some extra slashes. Try strip_slashes before json_decode.

Moe Sweet
  • 3,683
  • 2
  • 34
  • 46
1

Maybe you need to do urldecode? http://php.net/manual/en/function.urldecode.php

Eugene
  • 3,280
  • 21
  • 17
0

here is same error types

//wrong
$str1 = <<<EOD
{“dealList”:”\r\n\t”}
EOD;

//right
$str2 = <<<EOD
{“dealList”:”\\r\\n\\t”}
EOD;

//wrong
$str3 = <<<EOD
{‘dealList’:'\r\n’}
EOD;

//wrong
$str4 = <<<EOD
{‘dealList’:'\\r\\n’}
EOD;

//wrong
$str5 = “{‘dealList’:'\r\n’}”;

//wrong
$str6 = “{‘dealList’:'\\r\\n’}”;

//right
$str7 = ‘{“dealList”:”\r\n”}’;

$c = json_decode($str1);
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
  • ok, that seems reasonable. but if i'm using (JSON2.js)JSON.stringify(object) on the javascript side, i post that string through a hidden form field, how can i control it so i can get the correct string? – André Alçada Padez Nov 21 '11 at 14:10