0

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?

wheeeee
  • 1,046
  • 2
  • 14
  • 33
  • you have PHP installed and properly configured in your HTTP server, right – Jaromanda X Apr 19 '17 at 23:19
  • When you visit the page in a browser, does it run? – Kieran E Apr 19 '17 at 23:19
  • When you see code that is not within PHP tags on a .php page it echos that code into a String. – StackSlave Apr 19 '17 at 23:26
  • I am running everything on Ubuntu. When I visit the php page seperately, it runs fine. The entire php file is inside php tags. I do not have html or anything else in there. – wheeeee Apr 20 '17 at 00:08
  • @Barmar I think your link suggests that there's something wrong with my Apache, but my php runs fine when I access it from a global address. It's just screwing up when I try to call it from my javascript file. Also, I'm running everything locally on Ubuntu, which does not have a httpd.conf file, apparently. It supposedly uses an apache2.conf file, but I don't know how that's different from the httpd.conf, or how to change it without breaking it. – wheeeee Apr 20 '17 at 00:39
  • Take a look at item #6 in the first answer in the link. I suspect that's your problem. – Barmar Apr 20 '17 at 15:51
  • So you're saying that it never works if I access it via a local filepath, only through a global one? – wheeeee Apr 20 '17 at 15:55

0 Answers0