I have the following php object(??? new to this). It is being sent through from my page via AJAX and was sent over as JSON. I’m decoding it and trying to echo the results, but all I get is NULL. In Firebug, everything in the POST widow shows up fine, but nothing in the RESPONSE (which I assume is where I’m supposed to look?)
header('Content-type: application/json');
$res = json_decode($_POST['apiresponse'], true);
echo $res;
- The JSON object was passed to the above PHP file (also stringified before passing)
- JSON was decoded and saved as $res variable
- Everything shows fine in the Firebug POST window
How do I convert this into a PHP variable (string) that I can return to my original PHP page, and
How do I send / call the variable back?**
Thanks!
this was not the case: (skip down!)
how do you pass the variables from javascript on to php?
i’m really just guessing here, but it sounds a lot like you have something along the lines of
$.ajax( "/my-api-thing.php", { data: { "apiresponse": "some_value", "more":"things" } }).success(...);IF this is the case then you are only expecting json as a result from the php script, but you’re not actually sending a json object as a request, but normal http parameters.
you would then just write something like this in php:
$someVariable = $_POST["apiresponse"](btw: this would be the “normal” way to do it, sending json to the server is not all that common)
updated guess
i’ve verified the json you’re posting with this file:
so the json_decode doesn’t seem to be the problem at all.
a few other guesses:
to pinpoint the problem, can you run an
echo $_POST["apiresponse"];just before thejson_decodeto see if the output differs from the output?