Jump to content

Ajax


gerit99

Recommended Posts

My idea is to create a game with PHP,Javascipt and AJAX. But I have some trouble with AJAX. I've read W3schools.com and other pages, but i couldn't figure how I could change a database each time you click a button.<---------- My character btw :D

Edited by gerit99
  • Like 1
Link to comment
Share on other sites

I've made a script but it doesn't work. I've tried alot of things but it doesn't work, and since I'm at school i can't show my script. I've used _GET to read the info, and then i updated it.

Edited by gerit99
Link to comment
Share on other sites

we need to see the codes and error if any is there, to help you

Link to comment
Share on other sites

The problem is, there are no errors. Each keypress triggers this:

 var serverRequest=new XMLHttpRequest();function updatePlayerPosition(){serverRequest.open("GET","updatePositions.php?player=1&posTop="+document.getElementById('player').style.posTop+"&posLeft="+document.getElementById('player').style.posTop,true);serverRequest.send();}

updatePositions.php

<?PHP$player = $_GET["player"];$posTop = $_GET["posTop"];$posLeft = $_GET["posLeft"];mysql_query("UPDATE userplayers SET posTop=".$posTop.",posLeft=".$posLeft." WHERE playerID=".$player."");?>

And still I see this: posTop | posLeft 300 | 300

Edited by gerit99
Link to comment
Share on other sites

You're missing out really important pieces in the code you're presenting, but even from the little you're giving, it's clear that you're not setting the posTop and posLeft URL variables correctly.You have

serverRequest.open("GET","updatePositions.php?player=1&posTop="+document.getElementById('player').style.posTop+"&posLeft="+document.getElementById('player').style.posTop,true);

but there's no "posTop" property on the "style" object. The "style" object reflects the inline style (as you write them with the "style" attribute). From it, you have the "top" and "left" properties instead, i.e.

serverRequest.open("GET","updatePositions.php?player=1&posTop="+document.getElementById('player').style.top+"&posLeft="+document.getElementById('player').style.left,true);

Link to comment
Share on other sites

posTop works too, somehow. That's how i have been scripting it. I also need to paste the important pieces in my script
then as mentioned, please show ALL relevant code. what I don't get, is after the AJAX request get's made, what is supposed to happen? I don't see any callbacks being implemented, and nothing in the PHP code past the query.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...