...

View Full Version : Tell PHP if a key is up or down with Javascript?



BlackCow
08-21-2008, 04:14 AM
I know PHP well but I don't know much about Javascript. What I need to do is have Java Script detect if a key is down and tell PHP to do something when the key is down and then tell it to do something again when the key is back up.

I hope im not being to vague..

How would I go about doing this?

_Aerospace_Eng_
08-21-2008, 04:16 AM
You will need to use ajax to let the server know what is happening. Javascript can't send something directly to php without ajax. For a beginner its not going to be easy.

BlackCow
08-21-2008, 04:59 AM
You will need to use ajax to let the server know what is happening. Javascript can't send something directly to php without ajax. For a beginner its not going to be easy.

Is Ajax another language or is it related to Java Script? And could you give me a simple example? I'm not having much luck finding one on google.

Edit: Perhaps I could use Flash actionscript instead? I am more familiar with flash.

abduraooft
08-21-2008, 09:03 AM
Is Ajax another language or is it related to Java Script? And could you give me a simple example? I'm not having much luck finding one on google.
see http://en.wikipedia.org/wiki/AJAX
Sajax toolkit (http://www.modernmethod.com/sajax/) is fairly easy to customise.

barkermn01
08-21-2008, 11:34 AM
Flash Action Script 3 is verry simalar to Javascript
AJAX Basicly opens a Page without showing it no the Stages you can do is

Now Ajax has 3 main stages for you as a coder

1st, Connection to Page if you want to pass data to the page the easy way is to use $_GET[] so phpage.php?value1=value1

2nd is Waiting for the page ti send ready state so once php has done what it is supposed to

3rd Handle to Returned value

The Returned Value is any thing that is out putted i have once made a list that i seperateed using | then just do in javascript what you want to do with the value that is returned but to learn it

http://www.w3schools.com has some good tuts on how to implement AJAX

rnd me
08-21-2008, 09:53 PM
actually, you don't need to use ajax to talk TO a php server.
you can make get requests using image tags, its far simpler to setup and very reliable. its a mainstay of web traffic monitors and advertisers.


example i whipped up:


<body>
<input type="button"
value="php relay test"
onmousedown="phoneHome(1)"
onmouseup="phoneHome(0)" />

<script>

function phoneHome(buttonStatus){
var i = new Image;
var hitTime = (new Date()).getTime();
var myPhpUrl = "changeStatus.php"
i.src = myPhpUrl + "?button="+ buttonStatus + "&hitTime=" + hitTime;
}

</script>
</body>


you can then simply use the get/queryString features of php in your changeStatus.php (or whatever it is you are using) to catch the button parameter from the request. the example also passes the client time of the click, both for records and to guarantee a unique/uncached url for the request.

BlackCow
08-22-2008, 12:48 AM
actually, you don't need to use ajax to talk TO a php server.
you can make get requests using image tags, its far simpler to setup and very reliable. its a mainstay of web traffic monitors and advertisers.


example i whipped up:


<body>
<input type="button"
value="php relay test"
onmousedown="phoneHome(1)"
onmouseup="phoneHome(0)" />

<script>

function phoneHome(buttonStatus){
var i = new Image;
var hitTime = (new Date()).getTime();
var myPhpUrl = "changeStatus.php"
i.src = myPhpUrl + "?button="+ buttonStatus + "&hitTime=" + hitTime;
}

</script>
</body>


you can then simply use the get/queryString features of php in your changeStatus.php (or whatever it is you are using) to catch the button parameter from the request. the example also passes the client time of the click, both for records and to guarantee a unique/uncached url for the request.

I'm still not quite getting it. I don't understand what it means to use image tags and I cant find W3C documentation on it.

It dosn't seem like it should be to complicated. I just need Java to detect if I am holding lets say the "A" key down. If its held down then send the string "im holding the A key down" to a PHP document. When I bring the key back up it needs to send another string to the PHP document saying "Im not holding the A key down"

lokeshshettyk
08-25-2008, 04:47 PM
This may sound more like a tutorial, however this may help if used effectively:)
Try this : http://www.ajaxf1.com/tutorial/ajax-php.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum