Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    "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"

  • Users who have thanked devnull69 for this post:

    l33t H4X0R (04-19-2011)

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •