Sending data from AJAX call to PHP - can't read data
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sending data from AJAX call to PHP - can't read data
Hi,
Trying to send php file the data which was returned by a call to AJAX.
If I print_r($_GET) (returned from the php file attached below)
array Array ( [myData] => [object Object] )
If I var_dump($_GET) This is what is returned.
array array(1) { ["myData"]=> string(0) "" }
If I echo $_GET['Name'];
Notice: Undefined index: Name in /home/rick/DB-Web/db/Update.php on line 50 is returned.
If I echo $_GET[0]['Name'];
Notice: Undefined offset: 0 in /home/rick/DB-Web/db/Update.php on line 50
is returned
The data returned by AJAX (as shown by firebug ) is as follows:
[{"Name":"George Hopper","Nickname":"Hop","DOB":"1985-05-22","POB":"Koneville, MI","Birth_Record":"A-9023"
,"Recorded_Date":"1985-05-24","Birth_Certificate":"KM-B5099","SS_No":"145-39-9912","Mother_Name":"Alice
Jones Hopper","Mother_DOB":"1960-11-27","DL":"QT90-Idaho","DL_Expiry":"2020-11-17","Passport_No":"P01865"
,"PP_Expiry":"2020-08-15","Next_Kin":"Children: Pete Hopper, Mary Hopper Thompson, Willian J. Hopper"
}]
How can I retrieve the data to work with in the php file?
Code:
$('#rev').click(function () {
var param="update"
doAjax(param, nName);
function doAjax(param, nName) {
$.ajax({
type: "POST",
url: "getfields.php",
data: {"param":param, "nName":nName},
dataType: "json",
success: sendTbl
}); // ajax
function sendTbl(data) {
window.location.href="Update.php?myData="+data[0];
// can send data or data[0] same answer
} // sendTbl
} // doAjax
}); // #rev
// decode in associative array $myData = json_decode($_GET['myData'], true);
print_r($myData);
// or write it in a file file_put_contents('/path/to/writable/dir/debug.txt', print_r($myData, true)); ?>
Edit, reading the javascript code, it is not clear what you want to achieve...
functions should be written outside the click(function) block
I don't figure out the meaning of: window.location.href="Update.php?myData="+data[0];
...ok I get it, you want to send first element (= the whole data array/object from firebug output) of data array (retreived from ajax call) as GET method to Update.php when the ajax call to getfields.php is completed
Why not send data to Update.php with ajax as POST like in the first ajax call?
It is definitely useful to use the browser debugger ... "FireBug" in Firefox, or what have you ... to L-O-O-K ... A-T the actual data that is being sent and received. (By that I mean, "exactly what bytes were sent from the client to the host, and then, exactly what bytes came back?")
Armed with this information, you can do things such as "writing a little JavaScript program" (or maybe, doing it in the browser's console-window ...) that takes "this character string" and does with it whatever you think is supposed to happen next.
In this way, you ought to be able to fairly-quickly figure out where the problem lies.
The data returned by AJAX (as shown by firebug ) is as follows:
[{"Name":"George Hopper","Nickname":"Hop","DOB":"1985-05-22","POB":"Koneville, MI","Birth_Record":"A-9023"
,"Recorded_Date":"1985-05-24","Birth_Certificate":"KM-B5099","SS_No":"145-39-9912","Mother_Name":"Alice
Jones Hopper","Mother_DOB":"1960-11-27","DL":"QT90-Idaho","DL_Expiry":"2020-11-17","Passport_No":"P01865"
,"PP_Expiry":"2020-08-15","Next_Kin":"Children: Pete Hopper, Mary Hopper Thompson, Willian J. Hopper"
}]
in the AJAX success: it is referred to as 'data'
I want to send this to Update.php
How to do this?
Are you saying window.location.href="Update.php?myData="+data; won't work?
php sees the whole json object as a key in $_GET array, string(0) is buggous value
Also, there are some added underscores after commas (,_"DL":"QT90-Idaho...) maybe replacing spaces...
I think passing json array as get variable is not the way to go
Where does "myData" come from?
If you insist to pass json with get, you can convert your json data to get variables in javascript before calling window.location, like
Code:
var getDataArr = [];
for (var key in data) { // or data[0], I don't know how data is structured
getDataArr.push(key+'='+encodeURIComponent(data.key)); // or data[0].key
}
window.location.href="Update.php?"+getDataArr.join('&');
POST and GET variables are passed in the form: key1=value&key2=value&key3=value
So you code becomes:
Code:
$.each ...... {
uData.push(field+"="+encodeURIComponent(value));
}
// then convert to string by joining each uData element with a '&' character
var uDataString = uData.join('&');
// use this uDataString for data parameter value in your post ajax call
Thanks, keefaz, for your continued support. I really appreciate your staying with me on this and sorry that this is not getting resolved!!!
I'm beginning to think this is not just me being ignorant. I have done a fair amount of programming and have not had these problems before. I don't pretend to know much more than beginning intermediate, however.
I tried the call to Update.php with AJAX in two ways.
I did as you suggested in your last post with the results, case 1. and case 2.
In both cases the data posted to Update.php shows in firebug as data-as-posted.png.
I niether case does the data show up in the request.done function, showReturn.
function showReturn(data) {
alert("in showReturn "); <========== this does appear properly case 1. only
$.each(data, function (field, value) { <========== this does not appear creates TypeError: invalid in operand obj case 1.
alert(field+": "+value);
});
}
Comments appreciated.
R
case 1. Comment out - dataType:
var request = $.ajax({
type: "POST",
url: "Update.php",
data: uDataString //dataType: "json" dataType commented out
}); // ajax
response-notype.png
case 2. Using dataType: "json"
var request = $.ajax({
type: "POST",
url: "Update.php",
data: uDataString dataType: "json" dataType set to "json"
Yep it works, the data have been sent and received by Update.php, see Response tab
Now Update.php has to send data back with json type if your ajax expects json depending on dataType value, eg if you set dataType to json, the ajax callback expects a response encoded in json.
... in any such situation, start(!) by using the web-browser's debugging facilities to LQQK(!!) at the bytes(!) that the two computers-in-question are actually sending to one another.
If possible, do the same with any other, existing, interaction with this same web-server, which is known to already work, and that you are in the position to observe. ("Hey, d00d, they did it right. 'Go and do likewise!'")
If you confine yourself to looking at the situation only from the point-of-view of "one communicating party or the other," you cannot readily see "which one (or both?) of the two is screwing up!"
"You, as a human(!) outsider," can readily eavesdrop upon something that neither digital party can so-readily see. "Make the very best of it." (The hair follicles time you save will be your own.)
(And if you have not yet learned to prize "hair follicles," someday you will!)
Last edited by sundialsvcs; 06-16-2016 at 05:41 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.