cipherboy_loc
April 21st, 2011, 08:03 PM
If this is in the wrong forum, please move it.
I am currently working on developing a website which will require heavy MySQL data transfer. Because I don't want to have the MySQL server face outwards, I am using PHP scripts to get content from the server. I am also using a bit of JavaScript in various parts which require the MySQL content. My question is how do I use javascript to call a php file and return the contents?
I found the following code online, but the return statement doesn't work:
function getCorrectPick(quad, col, row) {
var txtFile = new XMLHttpRequest();
var allText = "";
txtFile.open("GET", "http://192.168.1.103/pbps/PHP5/cpicks.php?q=" + quad + "&c=" + col + "&r=" + row, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) {
allText = txtFile.responseText;
return allText;
} else {
return "Error!";
}
} else {
return "Error!";
}
}
txtFile.send(null);
return allText;
}
In the calling code, it calls the function, but the variable never receives the text:
var result = getCorrectPick(quad, col, row);
An alert just after this bit reveals that result is undefined, but the value of allText (in an alert) just before returning is the desired value. What is the issue?
Cipherboy
I am currently working on developing a website which will require heavy MySQL data transfer. Because I don't want to have the MySQL server face outwards, I am using PHP scripts to get content from the server. I am also using a bit of JavaScript in various parts which require the MySQL content. My question is how do I use javascript to call a php file and return the contents?
I found the following code online, but the return statement doesn't work:
function getCorrectPick(quad, col, row) {
var txtFile = new XMLHttpRequest();
var allText = "";
txtFile.open("GET", "http://192.168.1.103/pbps/PHP5/cpicks.php?q=" + quad + "&c=" + col + "&r=" + row, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) {
allText = txtFile.responseText;
return allText;
} else {
return "Error!";
}
} else {
return "Error!";
}
}
txtFile.send(null);
return allText;
}
In the calling code, it calls the function, but the variable never receives the text:
var result = getCorrectPick(quad, col, row);
An alert just after this bit reveals that result is undefined, but the value of allText (in an alert) just before returning is the desired value. What is the issue?
Cipherboy