Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Earth
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tell PHP if a key is up or down with Javascript?

    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?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Earth
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    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.
    Last edited by BlackCow; 08-21-2008 at 08:16 AM.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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 is fairly easy to customise.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    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

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    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:
    Code:
    <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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Earth
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    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:
    Code:
    <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"

  • #8
    Regular Coder lokeshshettyk's Avatar
    Join Date
    Aug 2008
    Location
    On the way to the moon!
    Posts
    157
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Code:
    This may sound more like a tutorial, however this may help if used effectively:)
    Try this : http://www.ajaxf1.com/tutorial/ajax-php.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •