1

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.

P.S. 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!

1 Answer 1

1

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.

E.g..

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

        }
        });
Sign up to request clarification or add additional context in comments.

2 Comments

i tryed it already, when i refresh the page to test it and it uploads the database with an empty value same as before
Yes. thats because you are sending ntng in your ajax request if you see properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.