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

1

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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);

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.