Form With Multiple Submit Buttons

683 views
Skip to first unread message

John

unread,
Oct 30, 2015, 3:50:47 PM10/30/15
to mojol...@googlegroups.com
I am on path of a UI that is a single form with submit buttons for
various functions. In a route I envision taking action based on what
button was selected.

From Tag Helpers I can see how to assign properties to submit button:

%= submit_button 'Ok!', id => 'foo'

However it is not clear how I can key off id in my route. The param
property doesn't seem to provide this information.

I am open to any suggestions including "don't do it that way do XXXX
instead" or "this is basic html. Learn that first".

Thanks,

John

Keita Sugama

unread,
Oct 30, 2015, 9:44:40 PM10/30/15
to mojol...@googlegroups.com
Hi John.

With this HTML

<input type="submit" name="button1" value="push it for foo">
<input type="submit" name="button2" value="push it for bar">

You will get URLs look like

?button1=push+it+for+foo

So you can detect which one has been pushed by checking the
existence of names in parameter.

john

unread,
Oct 31, 2015, 12:19:16 AM10/31/15
to mojol...@googlegroups.com
Perfect...thanks.
Message has been deleted

jay m

unread,
Oct 31, 2015, 12:30:02 PM10/31/15
to Mojolicious

the button value only shows up in your URL if your form method=GET which isn't recommended in any case where form submission changes your application's state (i.e. if it results in a database update) or if the submitted info is sensitive, or long

also, a better solution, whether using GET or POST, is to give both buttons the same name and different values, and check the value in the controller that handles the route

jay

john

unread,
Oct 31, 2015, 1:43:27 PM10/31/15
to mojol...@googlegroups.com
Thanks.  I gave that method a try and it works well.

    %= submit_button 'Submit', name => 'cmd', value => 'Play'
    %= submit_button 'Submit', name => 'cmd', value => 'Pause'

In my controller:

    my $cmd = lc($self->param('cmd'));

John
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages