简体   繁体   中英

Game project updating database using ajax

Im trying to make a realtime game with no page refresh. The problem is that i dont know what to do next or how to configure ajax script to update the mysql database when the player is movine over the map.

Here is the ajax code im trying to use.

     //calling ajax to update player location when he move around
        function send(url){
var request;
try{
    request= new XMLHttpRequest();
} catch (e){
    try{
        request= new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            request= new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            alert("Your browser broke!");
            return false;
        }
    }
}
request.onreadystatechange = function(){
    if(request.readyState == 4){
        //I dont know what to add here :-(
    }
}
request.open("GET", url, true);
request.send(null);
}
send("update_location.php?newX="+ toX + "&newY=" + toY)

update_location.php

  <?php
$new_x=$_GET['newX'];
$new_y=$_GET['newY'];
//echo"$new_x , $new_y";
$update_loc=mysql_query("UPDATE users SET location_x='$new_x' WHERE username='admin'");
?>

The main ideea is that when the player moves anywhere over the map the ajax updates new x and y values into the database.I dont need any button or jquery code added,i think ajax will work fine if somebody get me a hand doing this.

PS toX and toY are javascript vars wich i transformed to php vars so i can update them to mysql datavase.If somebody could help me do this i would really appreciate!

First sort out what you are trying to do in this case. From your description I dont think its clear.

If you want to do something based on your ajax response, then onSucess is the right option and not onreadystatechange .

If you want to do something on Ajax response, then you should send the required data from the server side and use it to perform required actions in your javascript code.

Eg.

new Ajax.Request('testurl',{
            method: 'post',
            parameters: {param1:"A", param2:"B", param3:"C"},
            onSuccess: function(response){
            //do something here

        }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM