Posting JSON from a hidden form field

I intercept a form post using jQuery. With form fields, I create a JSON object that is stored in a hidden form field. The value that is passed to the form field is similar to the following:

{"Status" : "Closed", "Location" : "Glasgow", "Date" : "2012-02-15"} 

But if I repeat the object from the $ _POST variable:

 echo $_POST['JSON']; 

Outputs the following:

 {\"Status\" : \"Closed\", \"Location\" : \"Glasgow\", \"Date\" : \"2012-02-15\"} 

I tried running this through stripslashes () and urldecode (), but I had no joy. I understand that I could just replace the backslashes with a replace function, but this is too much hacking.

Has anyone come across this failed json via mail before?

Note. . This is on the back of the Wordpress site. I am not sure if this will cause this effect.

+4
source share
4 answers

It looks like the server has magic_qoutes_gpc 'on'. (Http://www.php.net/manual/en/security.magicquotes.what.php)

+2
source

One day I ran into the same problem, and all I did was use JSON.stringify() to save it as a β€œString” in my hidden field and read the result using the jquery.parseJSON() method. Maybe this will help you! With stringify, you can also define a placeholder for your JSON object.
var myJSONText = JSON.stringify(myObject, replacer);

0
source

Although my English is not very good, but I see that the json problem is in php, you can use json_decode do, can be converted to an array

0
source

Another possibility you have is to encode url with encodeURIComponent() in javascript of your json object and urldecode() in php for the resulting object.

Keep in mind that encodeURIComponent () in js is not exactly the same as urlencode () in php, and like decodeURIComponent () does not match urldecode (), but in most cases, encoding in js and decoding in php and vice versa works well.

0
source

Source: https://habr.com/ru/post/1396167/