two or more buttons in a form; keyboard enter/return activates which button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ManWithNoName
    New Member
    • Aug 2007
    • 88

    two or more buttons in a form; keyboard enter/return activates which button

    If you have a form with x input and two or more buttons, how do you control which button has preceding when the user hits the keyboard key "enter"?

    Currently it seems to be determined in a hierarchical manner.

    E.g.

    [HTML]

    <form>

    <input />
    <input />

    <button name="1" value="a" />
    <button name="1" value="b" />
    <button name="1" value="c" />

    </form>

    [/HTML]

    When the user hits enter, button nr 1 will be activated, and value "a" will automatically be chosen as the default value (which is sent to PHP). How do you control this event so that button value "b" or "c" is activated instead?

    (comp. to a situation as '<input type="hidden" name="1" value="a"/><input type="text" name="1" value="b"/>' where b would be the default)

    I know you can handle this problem with javascript but I want to know if this can be handled with PHP.

    How did the "designers of the HTML form functionality" perceive the use of several buttons?
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by ManWithNoName
    If you have a form with x input and two or more buttons, how do you control which button has preceding when the user hits the keyboard key "enter"?

    Currently it seems to be determined in a hierarchical manner.

    E.g.

    [HTML]

    <form>

    <input />
    <input />

    <button name="1" value="a" />
    <button name="1" value="b" />
    <button name="1" value="c" />

    </form>

    [/HTML]

    When the user hits enter, button nr 1 will be activated, and value "a" will automatically be chosen as the default value (which is sent to PHP). How do you control this event so that button value "b" or "c" is activated instead?

    (comp. to a situation as '<input type="hidden" name="1" value="a"/><input type="text" name="1" value="b"/>' where b would be the default)

    I know you can handle this problem with javascript but I want to know if this can be handled with PHP.

    How did the "designers of the HTML form functionality" perceive the use of several buttons?
    PHP has nothing to do with how HTML behaves and submits the for. All PHP gets is an array of elements.

    In your example, its the browser behaving that way, there is a possibility some other browser submits the last button instead of the first when you press enter.

    This can be handled by javascript. Describe what factors are involved in deciding what button to press and we can help you in the javascript forum.

    Rov... please move this to JS forum to further disect this problem.

    If they don't help you there, PM me and I'll come there (I usually stay in PHP forum)

    Comment

    • ManWithNoName
      New Member
      • Aug 2007
      • 88

      #3
      Thanks for your quick reply and input.

      First, js is (currently) out of the question, this need to be on the server side.

      What I am trying to do is this:

      I have have a form with x steps.

      Each steps have a previous and next button.

      When/if the users presses "enter" on step 1, the user is taken to step 2.

      When/if the user presses "enter" in step 2, the user is now however taken back to step 1.

      The reason is that the previous button comes before the next button (the thing being people reading from left to right in this part of the world).

      I get the _POST correctly in php, and I could put a conditional statement and all... However, I do not know if the user has pressed the "enter" key or the submit button.

      I was hoping this could be done with hidden forms or something; I need to "catch" the enter key...

      Comment

      • ManWithNoName
        New Member
        • Aug 2007
        • 88

        #4
        This is the closest thing I found which implies that it is possible to do this:

        3. Test for form submission with a hidden element.

        Include a hidden variable named, say, _submit_check in your forms like this:

        <input type="hidden" name="_submit_c heck" value="1"/>

        Then, to test whether the form has been submitted, look for the _submit_check element in $_POST:

        if (array_key_exis ts('_submit_che ck', $_POST)) {
        /* ... do something with the form parameters ... */
        }

        Testing for the presence of a hidden element avoids problems that can result from browsers' varying behaviors when a user submits a form by pressing the Enter key instead of clicking a submit button.
        From http://www.onlamp.com/pub/a/php/2004...mhandling.html

        This should solve my problem:

        "Testing for the presence of a hidden element avoids problems that can result from browsers' varying behaviors when a user submits a form by pressing the Enter key instead of clicking a submit button"

        But I don't understad what they are talking about.

        I have a hidden input for the form name, but it will end up in the $_POST var regardless if the user presses submit or enter, no?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          The problem is, that not all browsers handle forms submitted this way in the same manner.
          IE, for example, will submit it without any of the submit buttons, while Firefox will submit by using the first button in the form.

          You could put a hidden element inside your form, and have each of your submit buttons change it's value via the onmousedown event before submitting.
          By using the onmousedown event, the value won't be changed unless the user actually presses the button. The onclick event won't work, as it is triggered by Firefox when the enter button is pressed.

          Then, using PHP, you could proceed based on the value of your hidden element.
          If the user submits by clicking enter, the default value of the hidden element would remain unchanged.

          Comment

          • ManWithNoName
            New Member
            • Aug 2007
            • 88

            #6
            Thanks for your input Atli!

            "By using the onmousedown event, the value won't be changed unless the user actually presses the button. The onclick event won't work, as it is triggered by Firefox when the enter button is pressed."

            I didn't know that, good stuff.

            All in all, interesting solution, but like I said earlier, I wanted to avoid all that is JS for this problem.

            The idea with "hidden forms" in that article I linked to is -- in my opinion -- all bollocks. However, it gave me another idea...

            I created a new button before my other buttons, named it X and hid it using CSS (display:none).

            Now when the user presses Enter in FF the x button is read and sent, I just pick it up.

            IE, as you pointed out, doesn't pick up the button when the user presses Enter, so I just check if X isset and if no; if no I'll know the user pressed Enter.

            I feel so smart ^.^

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              If you want to stick with your original idea that browsers will choose the first (submit) button then you could do just that.

              Put the next button first, however reload them with CSS.

              Can you atleast use CSS?

              Comment

              • ManWithNoName
                New Member
                • Aug 2007
                • 88

                #8
                Originally posted by dlite922
                If you want to stick with your original idea that browsers will choose the first (submit) button then you could do just that.

                Put the next button first, however reload them with CSS.

                Can you atleast use CSS?
                CSS is good to go.

                But what do you mean with "reload them with CSS" ???

                Comment

                Working...