473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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?
May 17 '08 #1
7 3118
dlite922
1,584 Expert 1GB
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)
May 17 '08 #2
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...
May 17 '08 #3
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_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/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?
May 17 '08 #4
Atli
5,058 Expert 4TB
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.
May 18 '08 #5
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 ^.^
May 18 '08 #6
dlite922
1,584 Expert 1GB
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?
May 20 '08 #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?
CSS is good to go.

But what do you mean with "reload them with CSS" ???
May 20 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Newbie | last post by:
I currently have a set of simple calculations to determine square footage and multuply that by a dollar amount per foot. I use form fields that are filled in by the user, and then the Submit...
3
by: BruceR | last post by:
I have posted a similar message and am reposting due to no response. Basically, I need to know why using the ampersand to indicate an access key for a button makes it so that simply entering the...
0
by: Hiroyuki Tanaka | last post by:
Hi, I am trying to develop an application for a touch screen using buttons for the numeric pad with Completion ComboBoxes. At the moment I am having a problem sending the button presses to my...
0
by: Hiroyuki Tanaka | last post by:
Hi All, I am trying to develop an application for a touch screen using buttons for the numeric pad with Completion ComboBoxes. At the moment I am having a problem sending the button presses to...
1
by: ravindradonkada | last post by:
Hi, I am Ravindra,presently doing a project in asp.net. The Login page of my Web Project consists of two Buttons. If user enters his username and password and clicks on enter button of keyboard,...
1
by: Peted | last post by:
I have a form with 8 checkbuttons in "button" apearence mode each button press activates a relay device on a connected piece of hardware. For the relay device to activate a specfic relay it...
6
by: Paul Furman | last post by:
I'm getting incorrect response when hitting the enter key instead of actually clicking the button on a form. It activates but the post data isn't being sent I think. The php generated page...
6
by: =?Utf-8?B?L2Rldi9udWxs?= | last post by:
Hello, i am using visual studio 2003 enterprise architect version. I am making apps for the .Net framework 1.1. While testing an interface, i discovered something strange. In this application...
6
by: Omicron | last post by:
Hello, Can someone lead me in the right direct with this request: Please look at the piece of code below: <table summary="Sort Buttons"> <tr> <td> <input onclick="justSong_filter()"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.