0

What I'm trying to do is use the below javascript to pull a php page into my html. This works, but what I need and struggling to do is use javascript to send a variable to php so I can use mysql to send results back.

httpRequest("garage-view-default.php", showdefaultimage);
function showdefaultimage(WIDGET){
    d = document.getElementById('garage-view-default');
    d.innerHTML = WIDGET;
}

function httpRequest(url, callback) {
    var httpObj = false;
    if (typeof XMLHttpRequest != 'undefined') {
        httpObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try{
            httpObj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {try{
            httpObj = new ActiveXObject('iMicrosoft.XMLHTTP');
        } catch(e) {}
    }
}
if (!httpObj) return;
httpObj.onreadystatechange = function() {
    if (httpObj.readyState == 4) { // when request is complete
        callback(httpObj.responseText);
    }
};
httpObj.open('GET', url, true);
httpObj.send(null);
}
4

4 回答 4

2

This is where you seem to be specifying the address of the PHP resource:

httpRequest("garage-view-default.php", showdefaultimage);

So you can just send values on the query string:

httpRequest("garage-view-default.php?someKey=someValue", showdefaultimage);

Then in PHP you'd find that value in $_GET["someKey"].

于 2013-07-18T13:10:25.603 回答
2

Relying on the actual code, just pass the var into URL:

garage-view-default.php?varName=varValue

So:

httpRequest("garage-view-default.php?varName=varValue", showdefaultimage);

And use that on your server like:

$var = $_GET['varName'];

But consider moving to a library like jQuery, because the final code will be much easier to read and maintain.

于 2013-07-18T13:11:01.773 回答
1

You can add a GET value to your url:

garage-view-default.php?key=value
于 2013-07-18T13:11:09.440 回答
0

You may add the parameter to your URL to read them with $_GET in php. You may also read the documentation of httpRequest on how to pass POST parameter if more content is required than what fits into GET.

于 2013-07-18T13:10:19.283 回答