This is probably easy but I could really use someone wiser in the ways of Ajax and JSON to help here. I have Javascript function passing an Ajax request to a PHP backend. I’m passing a javascript object as the “request_parameters” post variable. When this shows up on PHP I’m able to navigate it but the object structure has been replaced with arrays. Instead I’d like the object structure to remain intact so being the smart-enough-to-be-dangerous type I decide to JSON encode the object:
request_parameters: JSON.encode( requestObj );
versus just
request_parameters: requestObj;
On the PHP side I now get this as the value of the “request_parameters” variable (aka, $_POST[‘request_paramenters’]):
{\”forms\”:[{\”formName\”:\”main-form\”,\”formClass\”:\”lg-form main-form\”,\”formFields\”:[{\”id\”:\”activity-app_id\”,\”name\”:\”activity-app_id\”,\”value\”:\”0\”,\”type\”:\”hidden\”,\”startingValue\”:null},{\”id\”:\”activity-start_time\”,\”name\”:\”activity-start_time\”,\”value\”:\”\”,\”type\”:\”hidden\”,\”startingValue\”:null},{\”id\”:\”min-duration\”,\”name\”:\”min-duration\”,\”class\”:\”span1 form-ignore\”,\”value\”:\”50\”,\”type\”:\”text\”,\”startingValue\”:null},{\”id\”:\”activity-duration\”,\”name\”:\”activity-duration\”,\”value\”:\”3000000\”,\”type\”:\”hidden\”,\”startingValue\”:null},{\”id\”:\”exercise-distance\”,\”name\”:\”exercise-distance\”,\”class\”:\”input span1 left\”,\”value\”:\”25\”,\”type\”:\”text\”,\”startingValue\”:null},{\”id\”:\”exercise-distance-uom\”,\”name\”:\”exercise-distance_uom\”,\”value\”:\”miles\”,\”type\”:\”hidden\”,\”startingValue\”:null},{\”id\”:\”exercise-pace_average\”,\”name\”:\”exercise-pace_average\”,\”value\”:\”2\”,\”type\”:\”hidden\”,\”startingValue\”:null},{\”id\”:\”wp_posts-post_content\”,\”name\”:\”wp_posts-post_content\”,\”class\”:\”span5\”,\”value\”:\”super fast\”,\”type\”:\”textarea\”,\”startingValue\”:null}]}],\”action\”:\”went-running\”,\”post_type\”:\”exercise\”,\”primaryForm\”:0}
Ok, so this clearly got encoded so that the data would pass as a POST/GET variable without bad things happening. Fair enough. I figured I could address this on the PHP side by employing a decode before running the json_decode function. I first tried:
$request_parameters = json_decode( urldecode($request_parameters) );
Thought for sure this would be a winner. Nope. It didn’t give an error but $request_parameters seems to not be set to anything. Ok, then I went a little nuts and thought … “hey maybe the javascript used base64 encoding” … that led me to:
$request_parameters = json_decode( base64_decode($request_parameters) );
Bad juju. I’ll leave it there.
Any help would be greatly appreciated.
Try
json_decode(stripslashes($_POST['request_parameters']));You have probably got
magic_quotesenabled.[Edited]
If you have got Magic Quotes enabled, check the link provided in the comment below and disable it. If you use
stripslashesblindly and later change the server settings or move to a different server, the above code will break.If however disabling Magic Quotes is not a possible option in your case, then use the below code for stripping the slashes from this particular post parameter.