Getting Enter from keyborad insetead of $_POST

Discussion in 'PHP' started by babaMBA, Jul 16, 2007.

  1. #1
    I am using two forms on one .php page and both of them performing different functions.

    I can get the submit of the button in the form by

    if( $_post['btnSubmit'] )
    {
    do some thing
    }

    but in this case if user press enter button instead of clicking the submit button by mouse then the "do something " statments are not executed.

    Plz help me that how can i solve my problem, I want my code to be executed by both click of mouse on submit button and the enter from keyboard.

    Thx in Advance.
     
    babaMBA, Jul 16, 2007 IP
  2. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    If the user clicks "enter", the form submitted is the default form. If you have two forms on the page, that'll be a problem. How could you know which form the user meant to click?

    You could solve the problem by giving each form submit button a different name. So you'd have:

    <input type='submit' name='but1' value=' 1st form '>

    and

    <input type='submit' name='but2' value=' 2nd form '>

    then, in your php, just use:

    if($_POST["but1"]){
    execute first form
    }
    else{
    execute 2nd form
    }

    Still, if the user doesn't click on the submit button, how would you know which button he meant to click?
     
    NoamBarz, Jul 16, 2007 IP
  3. babaMBA

    babaMBA Guest

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I did the same
    if($_POST['btnSubmit'])
    {
    do something
    }

    but this works only with click on submit button and not with enter from keyboard.

    is there anymethod that can read the enter from keyboard and "do something" executed.
     
    babaMBA, Jul 16, 2007 IP
  4. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Use 'get' method instead of 'post' in the form tag, so that the form will be submitted whether the user clicks the button or press enter. Also, you need to change the if($_POST["but1"]) to if($_GET["but1"])
     
    srobona, Jul 16, 2007 IP