What I need is some AJAX script that could every few seconds check the output of a PHP file and If a button was clicked, tell that to the PHP file, and check the output again, without refreshing the page.
var button = 'notpressed';
var everyFewSeconds = 3;
var xmlhttp = null;
function buttonPressed() {
button = 'pressed';
}
function makeAjax() {
if(!window.XMLHttpRequest) {
alert('Your browser is outdated');
} else {
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'path/to/your.php?button=' + button, true);
xmlhttp.onreadystatechange = handleResponse;
xmlhttp.send();
button = 'notpressed';
}
}
makeAjax();
function handleResponse() {
if(xmlhttp.readyState==4) {
if(xmlhttp.status==200) {
// you can CHECK the output of the script here
// it is available as xmlhttp.responseText
}
setTimeout(makeAjax, everyFewSeconds * 1000);
}
}
HTML:
<input type="button" onclick="buttonPressed()"/ value="Press me!">
The button state will be available to PHP in $_GET["button"] ... it is either "pressed" or "notpressed"
Lovely, thanks a great deal.
What the PHP page will output will be just "yes" or "no" depending on whether it is your turn. What I need the JavaScript to do with that is to display some HTML (the button) if the output is "yes" and if "no" to display some other HTML.
Correction to my above post. I do need more help, I was thinking about making a new thread but it directly relates to this. How to I do more than one XMLHttpRequest? I have tried it by duplication the code and changing the variable name but it goes blank. Please help. I really like the the community in this forum so I hope I get response soon.