Multiple buttons in one form

81 views
Skip to first unread message

Maurice Ling

unread,
Sep 19, 2009, 10:45:19 PM9/19/09
to web2py-users
Hi everyone

May I know how to process multiple buttons on a form?
For example, I have 2 buttons (process and save). How should I write
the form.accept(request.vars, session) part to differentiate the
actions of the 2 buttons?

form = FORM(TABLE(TR('Sequence (raw format): ',
TEXTAREA(_type='text', _name='sequence',
requires=IS_NOT_EMPTY())),
TR('Action: ',
SELECT('Complementation', 'Transcribe',
'Translate',
'Back Transcribe', 'Back Translate',
_name='action'),
INPUT(_type='submit', _value='PROCESS'))))
if form.accepts(request.vars,session): ....

Thanks in advance
Maurice

weheh

unread,
Sep 19, 2009, 11:26:41 PM9/19/09
to web2py-users
Hmmm... tricky. My first inclination was to say, check the
request.vars to see if you can tease out which button was pressed. But
I looked in winpdb, myself, and I couldn't see any indication in any
of the variables as to which button was pressed (which isn't to say
that it's not there ... just that I couldn't find it). So then, I
thought, perhaps you should make 2 forms, putting one button in each
form. But that would mean that you'd have to duplicate the form data.
No good. So how about making the process option a checkbox and then
having a single submit button? Check to save only. Uncheck to process.
That would work, for sure, but maybe not acceptable to you.

weheh

unread,
Sep 19, 2009, 11:34:23 PM9/19/09
to web2py-users

weheh

unread,
Sep 19, 2009, 11:57:32 PM9/19/09
to web2py-users
OK, here's a way to do it:

{{=form.custom.begin}}
<button style="border: 1px solid blue;" type="submit"
name="process">Process</button>
<input value="default" type="hidden" name="myform" />
{{=form.custom.submit}}
{{=form.custom.end}}

Then, when you press the process button, the request.vars.process will
not be None.

Iceberg

unread,
Sep 20, 2009, 12:48:06 AM9/20/09
to web2py-users
Interesting. But is this trick universally reliable, or just depends
on different browsers?

Searching on the web and I confirmed my suspicion. This old post
mentioned "IE not sending the name/value pair for the submit button
when Enter was pressed after filling in a field in the form." And my
test confirmed that it is still the case even in today's IE7, which is
still the majority of browser in the market, huh?
http://muffinresearch.co.uk/archives/2005/12/08/fun-with-multiple-submit-buttons/

So I think we should not rely on this trick. If you really need
multiple submit buttons, try some javascript technique mentioned in
following post, although that way we need multiple actions in our
controller, which is not DRY enough. :-/
http://www.phpbuilder.com/board/showthread.php?t=10248489

Anybody who knows a better solution, please correct me. Thanks in
advance!

Regards,
Iceberg

PS: the test controller, if you like to test it by yourself.
def test():
form=FORM(
INPUT(_type='text',_name='foo'),
INPUT(_type='submit',_name='s1',_value='111'),
INPUT(_type='submit',_name='s2'),
)
if form.accepts(request.vars):
response.flash=str(request.vars)
return {'':form}

weheh

unread,
Sep 20, 2009, 2:27:22 AM9/20/09
to web2py-users
Curious. I originally tested my method on IE7 and it works OK.
Nevertheless, Iceberg is right in that the button tag isn't consistent
across browsers. w3cschools.com says
"""
Always specify the type attribute for the button. The default type for
Internet Explorer is "button", while in other browsers (and in the W3C
specification) it is "submit".
"""


On Sep 20, 12:48 am, Iceberg <iceb...@21cn.com> wrote:
> On Sep20, 11:57am, weheh <richard_gor...@verizon.net> wrote:
>
> > OK, here's a way to do it:
>
> > {{=form.custom.begin}}
> > <button style="border: 1px solid blue;" type="submit"
> > name="process">Process</button>
> > <input value="default" type="hidden" name="myform" />
> > {{=form.custom.submit}}
> > {{=form.custom.end}}
>
> > Then, when you press the process button, the request.vars.process will
> > not be None.
>
> Interesting. But is this trick universally reliable, or just depends
> on different browsers?
>
> Searching on the web and I confirmed my suspicion. This old post
> mentioned "IE not sending the name/value pair for the submit button
> when Enter was pressed after filling in a field in the form." And my
> test confirmed that it is still the case even in today's IE7, which is
> still the majority of browser in the market, huh?http://muffinresearch.co.uk/archives/2005/12/08/fun-with-multiple-sub...
>
> So I think we should not rely on this trick. If you really need
> multiple submit buttons, try some javascript technique mentioned in
> following post, although that way we need multiple actions in our
> controller, which is not DRY enough.  :-/http://www.phpbuilder.com/board/showthread.php?t=10248489

Iceberg

unread,
Sep 20, 2009, 3:20:18 AM9/20/09
to web2py-users
On Sep20, 12:48pm, Iceberg <iceb...@21cn.com> wrote:
> On Sep20, 11:57am, weheh <richard_gor...@verizon.net> wrote:
>
> > OK, here's a way to do it:
>
> > {{=form.custom.begin}}
> > <button style="border: 1px solid blue;" type="submit"
> > name="process">Process</button>
> > <input value="default" type="hidden" name="myform" />
> > {{=form.custom.submit}}
> > {{=form.custom.end}}
>
> > Then, when you press the process button, the request.vars.process will
> > not be None.
>
> Interesting. But is this trick universally reliable, or just depends
> on different browsers?
>
> Searching on the web and I confirmed my suspicion. This old post
> mentioned "IE not sending the name/value pair for the submit button
> when Enter was pressed after filling in a field in the form." And my
> test confirmed that it is still the case even in today's IE7, which is
> still the majority of browser in the market, huh?http://muffinresearch.co.uk/archives/2005/12/08/fun-with-multiple-sub...
>
> So I think we should not rely on this trick. If you really need
> multiple submit buttons, try some javascript technique mentioned in
> following post, although that way we need multiple actions in our
> controller, which is not DRY enough.  :-/http://www.phpbuilder.com/board/showthread.php?t=10248489
>

Correction. Proper javascript technique will NOT result in multiple
actions. We can still use one action to handle multiple relevant
submits. The point is to use multiple normal button to mimic multiple
submit button, yet still using only one real submit target.

def test():
form=FORM(
INPUT
(_type='hidden',_name='action',_id='action',_value='undefined'),
INPUT(_type='button',_value='Do something',_onclick='''
this.form.action.value=1;this.form.submit();
''',),
INPUT(_type='button',_value='Do something else',_onclick='''
this.form.action.value=2;this.form.submit();
''',),
)
if form.accepts(request.vars):
response.flash='You clicked button %s'%request.vars.action
return {'':form}

Regards,
Iceberg

Maurice Ling

unread,
Sep 20, 2009, 8:35:47 PM9/20/09
to web2py-users
Hi guys

Thanks a lot.

Maurice
> Iceberg- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages