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);
}
ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • Is using jQuery an option? It would be more quick and more clean. – Banning Jul 18 '13 at 13:09
  • What IE versions do you need to support? All that suff with `ActiveXObject` is only required if you want to support IE6 and IE5. If you've got any sense at all, you aren't supporting those old browsers, which means you can drop all that activeX junk. – Spudley Jul 18 '13 at 13:12
  • like this httpObj.open("GET","page.php?var="+var,true); – Bojan Kovacevic Jul 18 '13 at 13:12

4 Answers4

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"].

David
  • 208,112
  • 36
  • 198
  • 279
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.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
1

You can add a GET value to your url:

garage-view-default.php?key=value
randomizer
  • 1,619
  • 3
  • 15
  • 31
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.

eX0du5
  • 896
  • 7
  • 16