Vampyress86 Posted April 6, 2011 Share Posted April 6, 2011 Hey there, I am using prototype to send JSON information to a PHP file which will then use the data to update a database, the AJAX presently looks as follows: function saveprofile() { var email = document.getElementById("email").value; var profileDetails = { "email":email, "titleitalic":titleitalic, "profileDetails":titlefontcolor, "titlefontcolor":titleunderline, "titleoffsettop":titleoffsettop, "titleoffsetleft":titleoffsetleft, "aboutbackgroundcolour":aboutbackgroundcolour, "aboutunderline":aboutunderline, "aboutfontfamily":aboutfontfamily, "aboutbold":aboutbold, "aboutitalic":aboutitalic, "aboutfontcolor":aboutfontcolor, "aboutoffsettop":aboutoffsettop, "aboutoffsetleft":aboutoffsetleft, "statusoffsetleft":statusoffsetleft, "statusinfooffsetright":statusinfooffsetright, "pictureoffsetleft":pictureoffsetleft, "profilebold":profilebold, "profileunderline":profileunderline, "profileitalic":profileitalic, "profilefontcolor":profilefontcolor, "profilefontfamily":profilefontfamily, "profilebackgroundcolour":profilebackgroundcolour } var jsonstring = JSON.stringify(profileDetails); alert(jsonstring); var request = new Ajax.Request ('scripts/updateprofile.php', { method: 'POST', parameters: jsonstring, onComplete: responseReturned } ); } Which outputs the following JSON which has been tested on http://www.jsonlint.com/ as valid: { "email": "l-wilson-1986@hotmail.co.uk", "titleitalic": "normal", "profileDetails": "#d0deea", "titlefontcolor": "none", "titleoffsettop": 100, "titleoffsetleft": 100, "aboutbackgroundcolour": "transparent", "aboutunderline": "none", "aboutfontfamily": "Arial", "aboutbold": "normal", "aboutitalic": "normal", "aboutfontcolor": "#d0deea", "aboutoffsettop": 325, "aboutoffsetleft": 100, "statusoffsetleft": 450, "statusinfooffsetright": 250, "pictureoffsetleft": 100, "profilebold": "normal", "profileunderline": "none", "profileitalic": "normal", "profilefontcolor": "#fff", "profilefontfamily": "Arial", "profilebackgroundcolour": "#2c3c49" } The PHP file which receives the JSON looks as follows: <?php $myjson = $_POST['jsonstring']; $jsonchars = json_decode(($myjson), true); $json_errors = array( JSON_ERROR_NONE => 'No error has occurred', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', ); echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL; // do something with the data ?> The problem is the PHP constantly returns the following error: Last error : Syntax error and any attempts to access the JSON data returns NULL values. I have looked at several online examples and cannot see where I might be going wrong, and although users have had problems with syntax errors it normally relates to incorrect JSON. Any advice on this topic would be greatly appeciated Leanne Quote Link to comment https://forums.phpfreaks.com/topic/232882-problem-parsing-valid-json-in-php/ Share on other sites More sharing options...
kenrbnsn Posted April 6, 2011 Share Posted April 6, 2011 I tested with: <?php $json_str = '{ "email": "l-wilson-1986@hotmail.co.uk", "titleitalic": "normal", "profileDetails": "#d0deea", "titlefontcolor": "none", "titleoffsettop": 100, "titleoffsetleft": 100, "aboutbackgroundcolour": "transparent", "aboutunderline": "none", "aboutfontfamily": "Arial", "aboutbold": "normal", "aboutitalic": "normal", "aboutfontcolor": "#d0deea", "aboutoffsettop": 325, "aboutoffsetleft": 100, "statusoffsetleft": 450, "statusinfooffsetright": 250, "pictureoffsetleft": 100, "profilebold": "normal", "profileunderline": "none", "profileitalic": "normal", "profilefontcolor": "#fff", "profilefontfamily": "Arial", "profilebackgroundcolour": "#2c3c49" }'; $x = json_decode(($json_str),true); echo '<pre>' . print_r($x,true) . '</pre>'; $json_errors = array( JSON_ERROR_NONE => 'No error has occurred', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', ); echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL; ?> And it works fine. There has to be something wrong with the incoming string. If you have magic quotes turned on there are probably backslashes escaping the quotes in the string. Use stripslashes on it before passing it to json_decode <?php $myjson = stripslashes($_POST['jsonstring']); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/232882-problem-parsing-valid-json-in-php/#findComment-1197779 Share on other sites More sharing options...
Vampyress86 Posted April 6, 2011 Author Share Posted April 6, 2011 Hey Ken, Thankyou for your reply and my late response. It actually turns out the problem came from the #'s in the colour code, all this time and all I had to do was use var_dump($myjson); to see where my json string halted haha. Thankyou for your time and response Leanne Quote Link to comment https://forums.phpfreaks.com/topic/232882-problem-parsing-valid-json-in-php/#findComment-1197873 Share on other sites More sharing options...
blacknight Posted April 7, 2011 Share Posted April 7, 2011 preg_replace('/.+?({.+}).+/','$1',$json); usualy cleans it up to and stops errors... Quote Link to comment https://forums.phpfreaks.com/topic/232882-problem-parsing-valid-json-in-php/#findComment-1198006 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2011 Share Posted April 7, 2011 Can you explain what that does for those of us who are reg-expression challenged? Ken Quote Link to comment https://forums.phpfreaks.com/topic/232882-problem-parsing-valid-json-in-php/#findComment-1198046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.