matthewst Posted July 7, 2007 Share Posted July 7, 2007 I have a simple game script here http://matthews.gotdns.org/game/test_game.php test_game.php <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("test_game") or die(mysql_error()); //error_reporting(E_ALL); error_reporting(0); ?> <?php $query = "SELECT player_name, hit_points FROM users"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $player_name = $row['player_name']; $hit_points = $row['hit_points']; if($hit_points <= "0") { echo "$player_name is dead                  "; } else { echo "Player: $player_name "; echo "Hit points: $hit_points         "; } } ?> <title>test game</title> </head> <body> <br> <a href="atk.php?p=2">hit player two</a>                                     <a href="atk.php?p=1">hit player one</a> </body> </html> atk.php <?php mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("test_game") or die(mysql_error()); //error_reporting(E_ALL); error_reporting(0); ?> <?php if($_GET['p'] == 2){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; mysql_query($sql)or die(mysql_error()); } if($_GET['p'] == 1){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; mysql_query($sql)or die(mysql_error()); } echo "<script type='text/javascript'> <!-- window.location = 'test_game.php' //--> </script>"; ?> what I'm trying to accomplish is: I have the page loaded on my computer you have the page loaded on you computer I click "hit_player_2" and the page on your computer is updated (you lose a hit point) automatically without a refresh I don't wan't anyone to write the script for me (I can't learn that way). I just need someone to point me in a direction or give me a few key words to serach for. I've already read the tutorials but couldn't find anything pertaining to this problem. Quote Link to comment Share on other sites More sharing options...
matthewst Posted July 7, 2007 Author Share Posted July 7, 2007 I originally used a form but it didn't work as expected. It would take a point away from the wrong player the first time you clicked a button. <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("test_game") or die(mysql_error()); //error_reporting(E_ALL); error_reporting(0); ?> <?php $query = "SELECT player_name, hit_points FROM users"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $player_name = $row['player_name']; $hit_points = $row['hit_points']; if($hit_points <= "0") { echo "$player_name is dead                  "; } else { echo "Player: $player_name "; echo "Hit points: $hit_points         "; } } ?> <title>test game</title> </head> <body> <br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName"><input name="hit_player_2" type="submit" value="hit_player_2">                            <input name="hit_player_1" type="submit" value="hit_player_1"></form> <?php if(isset($_POST['hit_player_1'])){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; mysql_query($sql)or die(mysql_error()); } if(isset($_POST['hit_player_2'])){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; mysql_query($sql)or die(mysql_error()); } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
bibby Posted July 7, 2007 Share Posted July 7, 2007 matthewst, your php and mysql look just fine as your tests show. To have this dynamic processing both on the server side, and the client side without a refresh, you ought to lookup and realy get to know javascript. words to search for are javascript, events, onclick, ajax, xmlhttp; There is a thread with a lot of good code I participated in today that's worth a peek. http://www.phpfreaks.com/forums/index.php/topic,148418.0.html Javascript and the DOM have broken down the elements on a page to the point where one can query the server for a single value (like player1's HP) and replacing the content of an html element -> in this case perhaps, the html element that contains within it the current player1 HP (span, p, div). Quote Link to comment Share on other sites More sharing options...
matthewst Posted July 17, 2007 Author Share Posted July 17, 2007 ok, heres what i have now needless to say, it doesn't work game.php <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("test_game") or die(mysql_error()); //error_reporting(E_ALL); error_reporting(0); ?> <?php $query = "SELECT player_name, hit_points FROM users"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $player_name = $row['player_name']; $hit_points = $row['hit_points']; if($hit_points <= "0") { echo "$player_name is dead                  "; } else { echo "Player: $player_name "; echo "Hit points: $hit_points         "; } } ?> <head> <title>test game</title> <script language="javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('There was a problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(act) { // Open PHP script for requests http.open('get', 'atk.php?act='+act); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("atk").innerHTML = response; setTimeout(atk,5000); } } } </script> </head> <body> <br> <a href="atk.php?p=2">hit player two</a>                                     <a href="atk.php?p=1">hit player one</a> <?PHP function countPosts() { sendRequest('atk'); } ?> </body> </html> atk.php <?php mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("test_game") or die(mysql_error()); //error_reporting(E_ALL); error_reporting(0); ?> <?php if($_GET['p'] == 2){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; mysql_query($sql)or die(mysql_error()); } if($_GET['p'] == 1){ $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; mysql_query($sql)or die(mysql_error()); } echo "<script type='text/javascript'> <!-- window.location = 'test_game.php' //--> </script>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.