Jump to content

some ajax help please?


aloishis89

Recommended Posts

I built a version of the hak5 rover and I've been working on the code to drive it online. It works, but right now its a page written in PHP that has several submit buttons that execute different PHP functions, which make the robot move. The webpage is hosted on an onboard laptop, and you drive it by connecting to that onboard server. So even though it works right now, the nature of the submit buttons requires that the page get reloaded each time a command is sent. I want to also implement a failsafe in case the driver gets disconnected. The best way to do this would be with a socket connection (correct me if I'm wrong there). That way if the socket closes, then I can tell the onboard server to send a STOP to everything. I am also under the impression that that means I won't have to reload the page every time. Another reason I want to do this is because clicking buttons is not a very intuitive way to drive something, and I'd like to be able to use the keyboard (WASD). I already have a nice javascript library to do this for me, but the trouble is passing these variables of the states of the keys to the PHP that is controlling the motors. I have done a little research, and heard about this thing called COMET that is supposed to be good for opening a sustained connection between the server and the client. I was also wondering if it is possible to pass the keystroke information from javascript to PHP. Sorry for the length, but here is a sum up of my questions:

1) Is a socket connection a good idea for controlling a robot over the internet?

2) Can you pass information from javascript to PHP without having to reload the page?

3) Would COMET be useful in this situation?

I've never really used Ajax or anything before, so I'm learning it as I work on this project. Any help in the form of explanations or code snippits or links would be great. Thanks!

Link to comment
Share on other sites

You can pass code from javascript to PHP using AJAX. I would suggest reading up on JQuery as it will make you life easier and increase the chances of you code being browser independent.

Link to comment
Share on other sites

Ok, I got that much working. Now my problem is that I need to only have 1 character at any time in the input field. The only way I can think of doing this is by having javascript keep everything in the box always selected and making the maximum input length 1 so that when I type a new command, the old one gets overwritten (since it is selected). That way I don't have to use backspace or anything. So far I have only gotten as far as getting everything to select when you click in the box. I have this:

input type="text" id="txt1" size="30" maxlength="1" onkeyup="showHint(this.value)" onFocus="this.select()" onBlur="this.select()" onClick="SelectAll('txt1');" value="Click here, then press a key"/>

I have a couple different things going on in there to try to get it all selected (like the onFocus and the onBlur which don't work). Any idea how I could keep everything in that box permanently selected? Or is there a better way altogether to keep a single character in the box?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...