www.webdeveloper.com
Results 1 to 10 of 10

Thread: Reading+Processing PHP File outputs

  1. #1
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6

    Question Reading+Processing PHP File outputs

    I have resorted to using a forum after 2 days of head scratching. What I need is some script to monitor the output of a php file that outputs "yes" or "no" depending on weather it is your turn in a multiplayer game. I would like it to just check every few seconds (in order to not crash the server) if it has changed. While the PHP file is showing "yes" I want it to show some buttons to make a move and when it is showing "no" for it to just say something like "enemy's turn". I hear that this kind of thing could be achieved with jQuery and AJAX but I have never used JavaScript or AJAX before so that's why I've come here for help.

    I already feel like I'm making a big ask but if you don't mind I would also like a way of submitting your move to a php file to make the move (submitting something to MySql)

    I would really appreciate an answer to both however the first enquiry I consider a priority

  2. #2
    Join Date
    Apr 2011
    Location
    I live in the pinaple under the sea
    Posts
    107
    Ok its like going to somewhere place but you don't know where you should start.
    The first thing you should start is AJAX call

    Ajax is something like accessing server script without reloading page (no page load at all)

  3. #3
    Join Date
    Apr 2011
    Location
    I live in the pinaple under the sea
    Posts
    107
    Another things I have notice. based on what you want to happen. its somethings like you want a client side will response whenever a certain occurrence of a variable that is processing in the server become "YES" right?

    well thats a bit imposible...

    the reason why is because what you want to happen is like your walking upside-down

    in client-server application the server duty is to response to all the request coming from a requesting client browser. means that the browser part is the request and the server is the response. what you want to do is like you turn it upside down lol

    in ajax call the one will trigger some events is on the browser side (a javascript scripts) using ajax call that will call the php or what ever server side program then the server will response and that response will be available on your client side script (javascript)

  4. #4
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6
    the PHP file literally processes information and outputs to the user yes or no. As in if you were to visit the page all you would see in the browser would be "yes" or "no". All I want ajax to do is read it like a text file just checking it every so often.

  5. #5
    Join Date
    Apr 2011
    Location
    I live in the pinaple under the sea
    Posts
    107
    Just remember that the client always establish the request and everything will start from it (server will only response to the requesting client and then done)

    you can put the request by using any available events on the client side (like create a timer in javascript and establish a request)

  6. #6
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6
    that's kinda what I'm going for. PHP file figures out if it's you're turn and I want Javascript to do check whether it is outputting yes or no and act accordingly. I really need it to ping every 3 or 4 seconds.

  7. #7
    Join Date
    Apr 2011
    Location
    I live in the pinaple under the sea
    Posts
    107
    see this reference it will help you alot

    http://www.w3schools.com/PHP/php_ajax_intro.asp

  8. #8
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6
    I have looked at lots of reference sites but I am having great trouble understanding it. I would really appreciate some example source code

  9. #9
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6
    Just to help get the ball rolling a bit, this is some code I got off the internet and have attempted to adapt it. However, with just about everything I try, it totally failed.

    Code:
    		<div id="battle"></div>
    <script>
      // jQuery fires this function when the DOM is loaded and ready to use/manipulate
      $(document).ready(
         function(){
     
     (data)
            // via an HTTP GET action, and sets a function (callback) to run when it receives
            // a message back from the server
    	$.ajax({
    		url : "battlestatus.php",
    		success : function (data) {
    			yourTurn = html(data);
    			alert(yourTurn);
    			doReturnAction(yourTurn);
    		}
    	});
     
            
          }
      );
     
      function doReturnAction(data){
         if( data == "yes" ){
              $('#battle').html( 'HAHA!  Your Turn! GET HIM!<br><form><input name="attack" type="button" value="ATTACK!" /></form>' );
         }
         else{
              $('#battle').html( 'Please wait while you Enemy tries to kill you...' );
         }
      }
    </script>
    What I want this script to do:
    • Every few secconds check what output the php page is giving
    • display some HTML in the specified DIV if it outputs "yes"
    • and if it is outputting "no" display some different HTML


    I hope this clarifies what I need done
    EDIT: jQuery is included from an included php file containing page headers. I only displayed the piece I needed help with

  10. #10
    Join Date
    Apr 2011
    Location
    Apache
    Posts
    6
    is this thread dead because I really need help with this

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles