I am having a problem retrieving data out of an object passed back from PHP. I’ve tried many different ways to access this data and none work.
In Firebug I see the following… (it looks nicer in Firebug) – I tried to make this look as close to Firebug as possible
results Object { data=”{“formName”:”form3″,”formData”:”data goes here”}”, phpLiveDebug=”<…s: 198.91.215.227″}
data “{“formName”:”form3″,”formData”:”data goes here”}”
phpLiveDebug “<…s: 198.91.215.227”
I can access phpLiveDebug no problem, but the data portion is an object. I have tried the following…
success: function(results) {
//$("#formName").val(results.data.formName);
//$("#formName").val(results.data[0].formName);
//$("#formName").val(results.data[0]);
//$("#formName").val(results.data[1]);
//$("#formName").val(results.data[0]["formName"]);
var tmp = results.data[formName];
alert("!" + tmp + "!");
$("#formName").val(tmp);
$("#jqueryPHPDebug").val(results.phpLiveDebug);
}
This line works in the example above…
$("#jqueryPHPDebug").val(results.phpLiveDebug);
but… I can’t figure out how to get at the data inside the results.data portion… as you can see above, I have been trying different things and more not even listed there.
I was really hoping this line would work 🙂
var tmp = results.data[formName];
But it doesn’t. So, after many days of reading, tinkering, my solution was to re-write it to return data similar to the phpLiveDebug but then I thought… it’s gotta be something simple I’m overlooking…
Thank you for your time. Please try and explain why my logic (my horrible attempts at trying to figure out the proper method) above is wrong if you can?
UPDATE – this line here SHOULD have worked… but didn’t… here is why…
success: function(results) {
//$("#formName").val(results.data.formName);
Why that didn’t work…
In my function I had…
$jsonData["formName"] = $rowName; // the name of the form we are fetching
$jsonData["formData"] = "data goes here";
return (json_encode($jsonData));
How I called the function…
$dataReturn["data"] = $ds1->Invoke(rawurldecode($_POST["data"]));
if (DEBUGMODE) $dataReturn["phpLiveDebug"] = EH::CloseDebug();
exit (json_encode($dataReturn));
I was DOUBLE ENCODING the data… and for some reason…
$("#formName").val(results.data.formName);
Was not working.
I fixed the code by changing 1 line…
return (json_encode($jsonData));
to
return $jsonData;
Hope this helps someone!
I don’t know exactly what it did to not work but… it works now!
I wonder what happens internally when you encode an array and then add another array and RE encode that! Double encoding. My bad.
The keys are strings.
or