Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: ManWithNoName (Member) on May 17th, 2008 10:06 AM
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.

Code: ( text )
  1. <form>
  2.  
  3. <input />
  4. <input />
  5.  
  6. <button name="1" value="a" />
  7. <button name="1" value="b" />
  8. <button name="1" value="c" />
  9.  
  10. </form>


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?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
dlite922's Avatar
dlite922
Needs Regular Fix
491 Posts
May 17th, 2008
10:11 AM
#2

Re: two or more buttons in a form; keyboard enter/return activates which button
Quote:
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.

Code: ( text )
  1. <form>
  2.  
  3. <input />
  4. <input />
  5.  
  6. <button name="1" value="a" />
  7. <button name="1" value="b" />
  8. <button name="1" value="c" />
  9.  
  10. </form>


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)

Reply
ManWithNoName's Avatar
ManWithNoName
Member
73 Posts
May 17th, 2008
11:18 AM
#3

Re: two or more buttons in a form; keyboard enter/return activates which button
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...

Reply
ManWithNoName's Avatar
ManWithNoName
Member
73 Posts
May 17th, 2008
12:42 PM
#4

Re: two or more buttons in a form; keyboard enter/return activates which button
This is the closest thing I found which implies that it is possible to do this:

Quote:
Originally Posted by
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_check" value="1"/>

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

if (array_key_exists('_submit_check', $_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/200...rmhandling.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?

Reply
Atli's Avatar
Atli
Moderator
2,028 Posts
May 18th, 2008
02:29 PM
#5

Re: two or more buttons in a form; keyboard enter/return activates which button
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.

Reply
ManWithNoName's Avatar
ManWithNoName
Member
73 Posts
May 18th, 2008
07:31 PM
#6

Re: two or more buttons in a form; keyboard enter/return activates which button
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 ^.^

Reply
dlite922's Avatar
dlite922
Needs Regular Fix
491 Posts
May 20th, 2008
02:49 AM
#7

Re: two or more buttons in a form; keyboard enter/return activates which button
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?

Reply
ManWithNoName's Avatar
ManWithNoName
Member
73 Posts
May 20th, 2008
01:20 PM
#8

Re: two or more buttons in a form; keyboard enter/return activates which button
Quote:
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" ???

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,102 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors