I am working on a control panel for a web controlled robot. I'm trying to improve the way that I control it. I'd like to be able to use keystrokes, like WASD, to execute certain functions I've written in PHP (forward, left, back, right). I know I can detect what keys are being pressed using javascript, but I don't know how to pass this information to PHP without having to reload the page. I would also like to be able to implement a failsafe to shut down the robot when it loses the connection. I'm learning Ajax for this project as I go, so I'm pretty new to it. I've done a little research and heard about this thing called COMET that looked like it might be useful. So my questions are, is it possible to send keystrokes detected in javascript to PHP without reloading the page each time a keystroke is sent, and would something like COMET be useful in this situation?
You'll need to use AJAX to send the keystrokes back to PHP.
Could you be a little more specific? Maybe a link to an example or something similar?
You'll need an AJAX request: AJAX Tutorial
that triggers when the user presses a key: onkeypress Event
AJAX request looks like this:
for the php code: echo $_GET["wPressed"];Code:var j=new XMLHttpRequest(); var p="wPressed=true&sPressed=false&aPressed=false&dPressed=false"; var url="mypage.php?"+p; //local directory j.open("GET",url,false); j.send(null); document.write(j.responseText);
in this example, the responseText would be whether or not w was pressed. You'll have to implement yourself, but this is the basic concept. You don't need COMET for something like this. Use COMET when you need to be able to update a page in response to something on the server's end. If the chain is called from the client end though, there is no need.
Last edited by Jaan; 02-28-2010 at 05:20 AM. Reason: Please use code tags when you are posting your codes!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks