Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-19-2011, 06:43 AM   PM User | #1
l33t H4X0R
New to the CF scene

 
Join Date: Apr 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
l33t H4X0R is an unknown quantity at this point
Question AJAX Pulling from and Pushing to PHP

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.

Please Help, I'm a total JavaScript N00B.

P.S. If you need anymore details please ask
l33t H4X0R is offline   Reply With Quote
Old 04-19-2011, 12:21 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
"check the output" ... for what?

Generally you can do something like this:
Code:
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"
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
l33t H4X0R (04-19-2011)
Old 04-19-2011, 03:21 PM   PM User | #3
l33t H4X0R
New to the CF scene

 
Join Date: Apr 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
l33t H4X0R is an unknown quantity at this point
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.
l33t H4X0R is offline   Reply With Quote
Old 04-19-2011, 04:10 PM   PM User | #4
l33t H4X0R
New to the CF scene

 
Join Date: Apr 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
l33t H4X0R is an unknown quantity at this point
Just Declaring no further help is needed. I have managed to adapt the source code provided above and if working perfectly. Thanks a huge deal.
l33t H4X0R is offline   Reply With Quote
Old 04-22-2011, 09:18 PM   PM User | #5
l33t H4X0R
New to the CF scene

 
Join Date: Apr 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
l33t H4X0R is an unknown quantity at this point
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.
l33t H4X0R is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 AM.


Advertisement
Log in to turn off these ads.