Send form with jQuery using keypress()

Posted by kexxcream on Stack Overflow See other posts from Stack Overflow or by kexxcream
Published on 2012-04-01T19:04:12Z Indexed on 2012/04/02 23:29 UTC
Read the original article Hit count: 152

Filed under:
|
|
|

What I have so far:

I have a form in PHP that contain the following:

<input type="submit" id="0" name="0" value="This button">
<input type="submit" id="1" name="1" value="This button">
<input type="hidden" id="response" name="response" value="150">

The problem:

I would like to send the response value together with either name=1 or name=0, depending on which button the user submit. However, this should be done using the keyboard letters A and S. If a user press the letter A then the value 0 should be submitted, and if S is pressed then the value 1 should be sent.

The jQuery code:

$(document).ready(function()
{       
    // listens for any navigation keypress activity
    $(document).keypress(function(e)
    {
        switch(e.which)
        {
            // user presses the "a"
            case 97:    submitViaKeypress("0");
            break;  

            // user presses the "s" key
            case 115:   submitViaKeypress("1");
            break;
        }
    });
});

// shows a given element and hides all others
function submitViaKeypress(element_id)
{
    var response        = $('#response').attr('value');

    $.ajax({
        type: "POST",
        url: "initiate.php",
        data: "response=" + response + "&" + element_id + "=" + element_id
    });
}

Goal:

That "initiate.php receive two POST variables (response and either 0 or 1).

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery