How to submit a form when user presses Enter key

I'm pretty new at php and web stuff so please be gentle with me.

I'm trying to get a form to submit when the user presses the Enter
key. I do not want to use javascript. I've googled this to death and
cant find the answer (only hints), except on the 'Experts Exhange'
page and you have to pay to get the answer :-(

Why is this such a secret in the open source world we live in?

Daniel Klein
Daniel Klein [ Di, 15 Januar 2008 20:04 ] [ ID #1908582 ]

Re: How to submit a form when user presses Enter key

"Daniel Klein" <danielk [at] featherbrain.net> wrote in message
news:ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com...
> I'm pretty new at php and web stuff so please be gentle with me.
>
> I'm trying to get a form to submit when the user presses the Enter
> key. I do not want to use javascript. I've googled this to death and
> cant find the answer (only hints), except on the 'Experts Exhange'
> page and you have to pay to get the answer :-(
>
> Why is this such a secret in the open source world we live in?
>
> Daniel Klein

Hi,
If you have a form with a submit button, then ENTER will submit the
form as far as I know.
Not sure about all OS-en or browsers BTW.

Richard.
Csaba [ Di, 15 Januar 2008 20:07 ] [ ID #1908583 ]

Re: How to submit a form when user presses Enter key

Daniel Klein wrote:
> I'm pretty new at php and web stuff so please be gentle with me.
>
> I'm trying to get a form to submit when the user presses the Enter
> key. I do not want to use javascript. I've googled this to death and
> cant find the answer (only hints), except on the 'Experts Exhange'
> page and you have to pay to get the answer :-(
>
> Why is this such a secret in the open source world we live in?
>
> Daniel Klein

I think this is a browser specific thing.

Not sure, but some sites where the submit button don't work on firefox
or safari, will submit on safari, but not on firefox..

Sadly I suspect JavaShite is the only solution.
Courtney [ Di, 15 Januar 2008 20:17 ] [ ID #1908587 ]

Re: How to submit a form when user presses Enter key

On Tue, 15 Jan 2008 20:07:29 +0100, Richard wrote:

>
> "Daniel Klein" <danielk [at] featherbrain.net> wrote in message
> news:ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com...
>> I'm pretty new at php and web stuff so please be gentle with me.
>>
>> I'm trying to get a form to submit when the user presses the Enter
>> key. I do not want to use javascript. I've googled this to death and
>> cant find the answer (only hints), except on the 'Experts Exhange'
>> page and you have to pay to get the answer :-(
>>
>> Why is this such a secret in the open source world we live in?
>>
>> Daniel Klein
>
> Hi,
> If you have a form with a submit button, then ENTER will submit the
> form as far as I know.
> Not sure about all OS-en or browsers BTW.

I'd tend to agree with that... I use a function that prevents it from
doing so.

Experts Exchange is one of the sites in my blacklist... never trust a
technical site whose domain is pronounced "expert sexchange".


--
I told you this was going to happen.
Ivan Marsh [ Di, 15 Januar 2008 20:21 ] [ ID #1908589 ]

Re: How to submit a form when user presses Enter key

On Tue, 15 Jan 2008 20:07:29 +0100, "Richard" <root [at] localhost> wrote:

>
>"Daniel Klein" <danielk [at] featherbrain.net> wrote in message
>news:ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com...
>> I'm pretty new at php and web stuff so please be gentle with me.
>>
>> I'm trying to get a form to submit when the user presses the Enter
>> key. I do not want to use javascript. I've googled this to death and
>> cant find the answer (only hints), except on the 'Experts Exhange'
>> page and you have to pay to get the answer :-(
>>
>> Why is this such a secret in the open source world we live in?
>>
>> Daniel Klein
>
>Hi,
>If you have a form with a submit button, then ENTER will submit the
>form as far as I know.
>Not sure about all OS-en or browsers BTW.

Here is a extremely simplified example:

<html>
<body>
<h1>Test Page</h1>
<form action=test.php method="get">
<input type="submit" value="Submit">
</form>
</body>
</html>

So there is one 'submit' button. It appears that the button has to be
in focus in order for the user to be able to submit the form with the
Enter key. What am I missing? Is this even possible?

[ And let's not start another thread on Get vs Post :-) ]

Daniel Klein
Daniel Klein [ Di, 15 Januar 2008 20:21 ] [ ID #1908590 ]

Re: How to submit a form when user presses Enter key

On Tue, 15 Jan 2008 19:17:47 +0000, The Natural Philosopher <a [at] b.c>
wrote:

>Daniel Klein wrote:
>> I'm pretty new at php and web stuff so please be gentle with me.
>>
>> I'm trying to get a form to submit when the user presses the Enter
>> key. I do not want to use javascript. I've googled this to death and
>> cant find the answer (only hints), except on the 'Experts Exhange'
>> page and you have to pay to get the answer :-(
>>
>> Why is this such a secret in the open source world we live in?
>>
>> Daniel Klein
>
>I think this is a browser specific thing.
>
>Not sure, but some sites where the submit button don't work on firefox
>or safari, will submit on safari, but not on firefox..
>
>Sadly I suspect JavaShite is the only solution.

Ok, that makes sense. I have been testing on Firefox and Opera (I hate
IE). Safari uses too much memory on Windows so I am waiting for the
next release.

....and I have the same opinion as you on javascript ;-)

Daniel Klein
Daniel Klein [ Di, 15 Januar 2008 20:26 ] [ ID #1908592 ]

Re: How to submit a form when user presses Enter key

"Daniel Klein" <danielk [at] featherbrain.net> wrote in message
news:g91qo3tkqqv777bd6elc5ercea91iddl7r [at] 4ax.com...
> On Tue, 15 Jan 2008 20:07:29 +0100, "Richard" <root [at] localhost>
> wrote:
>
>>
>>"Daniel Klein" <danielk [at] featherbrain.net> wrote in message
>>news:ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com...
>>> I'm pretty new at php and web stuff so please be gentle with me.
>>>
>>> I'm trying to get a form to submit when the user presses the Enter
>>> key. I do not want to use javascript. I've googled this to death
>>> and
>>> cant find the answer (only hints), except on the 'Experts Exhange'
>>> page and you have to pay to get the answer :-(
>>>
>>> Why is this such a secret in the open source world we live in?
>>>
>>> Daniel Klein
>>
>>Hi,
>>If you have a form with a submit button, then ENTER will submit the
>>form as far as I know.
>>Not sure about all OS-en or browsers BTW.
>
> Here is a extremely simplified example:
>
> <html>
> <body>
> <h1>Test Page</h1>
> <form action=test.php method="get">
> <input type="submit" value="Submit">
> </form>
> </body>
> </html>
>
> So there is one 'submit' button. It appears that the button has to
> be
> in focus in order for the user to be able to submit the form with
> the
> Enter key. What am I missing? Is this even possible?
>
> [ And let's not start another thread on Get vs Post :-) ]
>
> Daniel Klein

I think its enough for the form to have the focus (if there is such a
thing).
If you add a text input, and type something in there, then try the
ENTER key.
What is a form without fields?
:)
Richard.
Csaba [ Di, 15 Januar 2008 20:27 ] [ ID #1908593 ]

Re: How to submit a form when user presses Enter key

In article <ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com>,
Daniel Klein <danielk [at] featherbrain.net> wrote:
>I'm pretty new at php and web stuff so please be gentle with me.
>
>I'm trying to get a form to submit when the user presses the Enter
>key.

A browser should do this automatically on any form that contains a
text input field <input type="text" ...>. If you press Enter after
typing something in the field, the form should submit. Nearly all
browsers I have used do this.

I don't know why you'd want it on forms without a text input field,
even with Javascript. As a user, *I* certainly don't want to be
submitting a form if I happen to press the Enter key.

>Why is this such a secret in the open source world we live in?

It's no secret. It's built into the browser.

-A
axlq [ Di, 15 Januar 2008 20:30 ] [ ID #1908594 ]

Re: How to submit a form when user presses Enter key

Daniel Klein wrote:

> What am I missing?

A text field.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
ivansanchez-alg [ Di, 15 Januar 2008 20:32 ] [ ID #1908596 ]

Re: How to submit a form when user presses Enter key

On Tue, 15 Jan 2008 19:30:40 +0000 (UTC), axlq [at] spamcop.net (axlq)
wrote:

>In article <ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com>,
>Daniel Klein <danielk [at] featherbrain.net> wrote:
>>I'm pretty new at php and web stuff so please be gentle with me.
>>
>>I'm trying to get a form to submit when the user presses the Enter
>>key.
>
>A browser should do this automatically on any form that contains a
>text input field <input type="text" ...>. If you press Enter after
>typing something in the field, the form should submit. Nearly all
>browsers I have used do this.
>
>I don't know why you'd want it on forms without a text input field,
>even with Javascript. As a user, *I* certainly don't want to be
>submitting a form if I happen to press the Enter key.
>
>>Why is this such a secret in the open source world we live in?
>
>It's no secret. It's built into the browser.

I agree, but the problem is a little deeper than that. What if there
are 2 (or more) submit buttons. What html needs to be there to
indicate the 'default' button?

Daniel Klein
Daniel Klein [ Di, 15 Januar 2008 20:35 ] [ ID #1908597 ]

Re: How to submit a form when user presses Enter key

Daniel Klein wrote:
> On Tue, 15 Jan 2008 19:30:40 +0000 (UTC), axlq [at] spamcop.net (axlq)
> wrote:
>
>
>> In article <ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com>,
>> Daniel Klein <danielk [at] featherbrain.net> wrote:
>>
>>> I'm pretty new at php and web stuff so please be gentle with me.
>>>
>>> I'm trying to get a form to submit when the user presses the Enter
>>> key.
>>>
>> A browser should do this automatically on any form that contains a
>> text input field <input type="text" ...>. If you press Enter after
>> typing something in the field, the form should submit. Nearly all
>> browsers I have used do this.
>>
>> I don't know why you'd want it on forms without a text input field,
>> even with Javascript. As a user, *I* certainly don't want to be
>> submitting a form if I happen to press the Enter key.
>>
>>
>>> Why is this such a secret in the open source world we live in?
>>>
>> It's no secret. It's built into the browser.
>>
>
> I agree, but the problem is a little deeper than that. What if there
> are 2 (or more) submit buttons. What html needs to be there to
> indicate the 'default' button?
>
> Daniel Klein
>

First off .... It's IE that does not submit with Enter. Firefox does.

As far as I know there is no way to "indicate" the default button. If
there are two submit buttons, the first is submitted (by Firefox).

In that case, if necessary, I use JavaScript on text fields to disable
Enter from submitting.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Nothing he's got he really needs
Twenty first century schizoid man.
***********************************
Chuck Anderson [ Di, 15 Januar 2008 20:55 ] [ ID #1908600 ]

Re: How to submit a form when user presses Enter key

On Jan 15, 12:04 pm, Daniel Klein <dani... [at] featherbrain.net> wrote:
> I'm pretty new at php and web stuff so please be gentle with me.
>
> I'm trying to get a form to submit when the user presses the Enter
> key. I do not want to use javascript. I've googled this to death and
> cant find the answer (only hints), except on the 'Experts Exhange'
> page and you have to pay to get the answer :-(
>
> Why is this such a secret in the open source world we live in?
>
> Daniel Klein

If I remember correctly, enter as a submit button trigger only works
if the form has only 1 element besides a submit button (ie, a single
text field) or javascript using a keylistener.

As for Experts Exchange, it's pretty much an open secret that if you
use google and get an EE result, all you need do is click the cached
link and the answers will be visible :)
Logos [ Mi, 16 Januar 2008 00:34 ] [ ID #1908629 ]

Re: How to submit a form when user presses Enter key

On Tue, 15 Jan 2008 20:35:17 +0100, Daniel Klein =

<danielk [at] featherbrain.net> wrote:

> On Tue, 15 Jan 2008 19:30:40 +0000 (UTC), axlq [at] spamcop.net (axlq)
> wrote:
>
>> In article <ts0qo3p6i33dh14e9aoookeldba13g1g3h [at] 4ax.com>,
>> Daniel Klein <danielk [at] featherbrain.net> wrote:
>>> I'm pretty new at php and web stuff so please be gentle with me.
>>>
>>> I'm trying to get a form to submit when the user presses the Enter
>>> key.
>>
>> A browser should do this automatically on any form that contains a
>> text input field <input type=3D"text" ...>. If you press Enter after
>> typing something in the field, the form should submit. Nearly all
>> browsers I have used do this.
>>
>> I don't know why you'd want it on forms without a text input field,
>> even with Javascript. As a user, *I* certainly don't want to be
>> submitting a form if I happen to press the Enter key.
>>
>>> Why is this such a secret in the open source world we live in?
>>
>> It's no secret. It's built into the browser.
>
> I agree, but the problem is a little deeper than that. What if there
> are 2 (or more) submit buttons. What html needs to be there to
> indicate the 'default' button?

An hidden input, with default value? Firefox and some others will indeed=
=

use the first submit button they encounter, for others the hidden input =
=

can tell you what to do. It requires some extra care, and if you've only=
=

one script processing only one particular form, this is offcourse not =

needed, as you can have your default defined in PHP. However, if you hav=
e =

some more complicated framework hidden inputs can help you a lot:

Form:
<input type=3D"hidden" name=3D"defaultaction" value=3D"edit" >
<input type=3D"submit" name=3D"action[edit]" value=3D"Edit this" >
<input type=3D"submit" name=3D"action[remove]" value=3D"Delete" >

(By the way, offcourse you can use the values of submit buttons instead =
of =

using a pseudo array, but normally I'd like a little more freedom in =

those, as they're displayed text. They may be translated, or altered on =
a =

request of a client. This way the underlying logic would remain the same=
).

Script:
<?php
$actions =3D array();
if(isset($_POST['action']) && is_array($_POST['action'])){
$actions =3D array_keys($_POST['action']);
} else if(isset($_POST['defaultaction'])){
$actions =3D array($_POST['defaultaction']);
} else {
//no action given, depends on your design what to do.
}
foreach($actions as $action){
switch($action){
case 'edit':
//code
break;
case 'edit':
//code
break;
default:
//unknown action, decide how to handle this
}
}
?>

This example would be of a framework that is able to handle different =

requests in one go BTW, if you need to have only one action valid, it =

could easily be altered to fit that. If you do use the values of a submi=
t =

button, and only one action is legal, the fact that later input with the=
=

same name overwrite the previous one will help you:

<form>
<input type=3D"hidden" name=3D"action" value=3D"edit">
....some other non-submit inputs
<input type=3D"submit" name=3D"action" value=3D"edit">
<input type=3D"submit" name=3D"action" value=3D"delete">
</form>

And independently of UA, if the form is submitted by enter 'edit' will =

always be chosen.
-- =

Rik Wasmus
luiheidsgoeroe [ Mi, 16 Januar 2008 00:42 ] [ ID #1908630 ]

Re: How to submit a form when user presses Enter key

The Natural Philosopher wrote:
> Daniel Klein wrote:
>> I'm pretty new at php and web stuff so please be gentle with me.
>>
>> I'm trying to get a form to submit when the user presses the Enter
>> key. I do not want to use javascript. I've googled this to death and
>> cant find the answer (only hints), except on the 'Experts Exhange'
>> page and you have to pay to get the answer :-(
>>
>> Why is this such a secret in the open source world we live in?
>>
>> Daniel Klein
>
> I think this is a browser specific thing.
>
> Not sure, but some sites where the submit button don't work on firefox
> or safari, will submit on safari, but not on firefox..
>
> Sadly I suspect JavaShite is the only solution.
>
>
>
>

By the way, Experts Exchange is pretty silly. If you found the Experts
Exchange by Google, just click the 'Cache' link and you will see the
answers without having to pay :)

--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel [at] fanetworks.net
Daniel Ennis [ Mi, 16 Januar 2008 04:18 ] [ ID #1909674 ]

Re: How to submit a form when user presses Enter key

On Jan 15, 11:04 am, Daniel Klein <dani... [at] featherbrain.net> wrote:
> I'm pretty new at php and web stuff so please be gentle with me.
>
> I'm trying to get a form to submit when the user presses the Enter
> key. I do not want to use javascript. I've googled this to death and
> cant find the answer (only hints), except on the 'Experts Exhange'
> page and you have to pay to get the answer :-(
>
> Why is this such a secret in the open source world we live in?
>
> Daniel Klein

check this

<script type="text/javascript">
function submit_form(frm)
{
if(event.key==13)
{
document.getElementById(frm).submit();
}
}

</script>


<form name="myform" id="myform" action="">
<input type="text" name="username" onkeydown="submit_form('myform')">
<input type="password" name="password"
onkeydown="submit_form('myform')">
<input type="submit" name="Submit" value="Submit">
</form>
radmission05 [ Mi, 16 Januar 2008 15:53 ] [ ID #1909718 ]

Re: How to submit a form when user presses Enter key

On Jan 15, 11:04 am, Daniel Klein <dani... [at] featherbrain.net> wrote:
> I'm pretty new at php and web stuff so please be gentle with me.
>
> I'm trying to get a form to submit when the user presses the Enter
> key. I do not want to use javascript. I've googled this to death and
> cant find the answer (only hints), except on the 'Experts Exhange'
> page and you have to pay to get the answer :-(
>
> Why is this such a secret in the open source world we live in?
>
> Daniel Klein

remember key 13, is keyboard key for Enter/ Carriage return.
radmission05 [ Mi, 16 Januar 2008 15:55 ] [ ID #1909719 ]

Re: How to submit a form when user presses Enter key

radmission05 [at] gmail.com ha scritto:
> On Jan 15, 11:04 am, Daniel Klein <dani... [at] featherbrain.net> wrote:
>> I'm pretty new at php and web stuff so please be gentle with me.
>>
>> I'm trying to get a form to submit when the user presses the Enter
>> key. I do not want to use javascript. I've googled this to death and
>> cant find the answer (only hints), except on the 'Experts Exhange'
>> page and you have to pay to get the answer :-(
>>
>> Why is this such a secret in the open source world we live in?
>>
>> Daniel Klein
>
> check this
>
> <script type="text/javascript">
> function submit_form(frm)
> {
> if(event.key==13)
> {
> document.getElementById(frm).submit();
> }
> }
>
> </script>
>
>
> <form name="myform" id="myform" action="">
> <input type="text" name="username" onkeydown="submit_form('myform')">
> <input type="password" name="password"
> onkeydown="submit_form('myform')">
> <input type="submit" name="Submit" value="Submit">
> </form>

This is HTML!
Whenever you type the ENTER key on a single <input> field you achieve a
SUBMIT as long as: a) the form has only that field; *or* b) the form has
an <input type="submit"> on it.

have fun!
Bob [ Fr, 18 Januar 2008 12:52 ] [ ID #1911511 ]
PHP » comp.lang.php » How to submit a form when user presses Enter key

Vorheriges Thema: Project directory
Nächstes Thema: Scan web pages and compose summary