I'm trying to use XmlHttpRequest to run a php script from a javascript file. But for some reason, my code is only returning the php code as a string instead of actually running it.
function drawOutput(responseText) {
console.log(responseText);
}
function drawError(status) {
console.log('Error: ' + status);
}
// handles the response, adds the html
function getRequest(url, success, error) {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
console.log("XML request failed.");
return false;
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
if(req.readyState == 4) {
return req.status === 200 ? success(req.responseText) : error(req.status);
}
}
req.open("GET", url, true);
req.send(null);
return req;
}
var urlString = '../tests/page_load.php';
//var urlString = 'server_scripts/page_load.php?' + encodeURIComponent('game_id') + '=0&' + encodeURIComponent('client_timestamp') + '=10';
//var urlString = "http://rpal.cs.cornell.edu/YH/public/tests/server_scripts/page_load.php?" + encodeURIComponent('game_id') + '=0&' + encodeURIComponent('client_timestamp') + '=10';
//var urlString = 'http://localhost/YH/WebHanabi/public/tests/server_scripts/page_load.php&' + encodeURIComponent('game_id') + '=0&' + encodeURIComponent('client_timestamp') + '=10';
console.log(urlString);
var urlRequest = getRequest(
urlString, // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
Edit: The php will run fine if I plug its url straight into the url bar. But it won't run when I try to call it from the javascript. Currently, all the javascript does is get the text of my php file and dumps the code into console. How do I make it so that the php code actually runs?