You are here: Re: How to submit a form when user presses Enter key « PHP Programming Language « IT news, forums, messages
Re: How to submit a form when user presses Enter key

Posted by Rik Wasmus on 10/22/58 12:00

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

<danielk@featherbrain.net> wrote:

> On Tue, 15 Jan 2008 19:30:40 +0000 (UTC), axlq@spamcop.net (axlq)
> wrote:
>
>> In article <ts0qo3p6i33dh14e9aoookeldba13g1g3h@4ax.com>,
>> Daniel Klein <danielk@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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация