# Two submit buttons

**URL:** <https://www.ruby-forum.com/t/two-submit-buttons/81368>\
**Category:** Rails\
**Created:** [January 8, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368 "2007-01-08T15:06:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 8, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/1 "2007-01-08T15:06:39Z")

</div>

I am using two submit buttons within my form without any problem, as  
specified ([http://www.railsdiary.com/diary/two\_submit\_buttons](http://www.railsdiary.com/diary/two_submit_buttons))

BUT when the user doesn’t hit a button and just hit ENTER, I got the  
first button submit in my controller which unfortunatly is the Cancel  
button in my view…

Yes, if I change the button position (OK first in html…) I get it… but  
I’d like to keep my display as required by the users… Cancel button  
must come before OK button…

 \<%= submit\_tag "Cancel", {:name =\> "submit", :class =\> "inputSubmit" }%\> \<%= submit\_tag "OKÂ»", {:name =\> "submit", :class =\> "inputSubmit"} %\>

is ther any way to do that ?

thanks a lot fyl

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 8, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/2 "2007-01-08T15:19:43Z")

</div>

Could you not use CSS to position the buttons in another order?

For example, one of:

div.submit div {  
position: relative;  
}

/\* you would need to define separate classes for the ok and cancel  
buttons \*/

div.submit div .okButton {  
position: absolute;  
top: 0px;  
left: 0px;  
}

div.submit div .cancelButton {  
position: absolute;  
top: 0px;  
left: 100px; /\* or however wide your buttons are \*/  
}

Alternatively:

div.submit div {  
float: right;  
}

I haven’t tested either of these, but you should be able to do  
something similar to swap the button order.

Cheers,  
Roland

On Jan 8, 2:06 pm, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/3 "2007-01-19T16:30:05Z")

</div>

Try setting the tab order using tabindex:

\<%= image\_submit\_tag( “buttons/ok.gif”,  
:border =\> “0”,  
:title =\> ‘Submit page’,  
:id =\> ‘ok\_button’,  
:tabindex =\> 13) %\>

Just make sure that your OK button is earlier in the tabindex order than  
the Cancel button.

Here are some references:

[http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1](http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1)  
[http://www.htmlcodetutorial.com/forms/\_INPUT\_TABINDEX.html](http://www.htmlcodetutorial.com/forms/_INPUT_TABINDEX.html)

David

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/4 "2007-02-15T21:55:11Z")

</div>

On this two submit button subject, have anyone successfully tried the  
“:name” technique in a form\_remote\_tag case? In the following example,  
params[:preview] and params[:ok] are both present, so can’t tell which  
button is clicked, any idea how to get around this?

\<%= form\_remote\_tag …\>  
\<image\_submit\_tag(“ok.gif”, :name=\>“ok”)%\>  
\<image\_submit\_tag(“preview.gif”, :name=\>“preview”)%\>  
\<%= end\_form\_tag %\>

thanks

Oliver

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/5 "2007-02-15T22:05:12Z")

</div>

if i remember correctly, the way around this is to have a hidden field  
in the form and when you click the submit button, you set the value on  
the hidden field to indicate what button was clicked. you can then  
check this hidden field value on the server.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/6 "2007-02-15T22:38:16Z")

</div>

hmm … not sure why this can work. If I understand it correctly, A  
hidden field is part of the submitted form, not associated with the  
submit button. \<%= hidden\_field\_tag … %\> will be submitted along  
with the rest of the form, but still not sufficient to make a  
distinction. Maybe I get you wrong, an example will be good.

thanks for the reply.

Oliver

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 16, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/two-submit-buttons/81368/7 "2007-02-16T12:32:29Z")

</div>

off the top of my head…not tested. not even sure if you need the  
submit() call in the onclicks as it may still get submitted because of  
the submit buttons. give it a shot.

\<%= form\_remote\_tag …, :html =\> { :id =\> ‘my\_form’ } %\>  
…  
\<%= submit\_tag “Delete”, :onclick =\> “$(‘form\_action’).value =  
‘delete’;$(‘my\_form’).submit()” %\>  
\<%= submit\_tag “Update”, :onclick =\> “$(‘form\_action’).value =  
‘update’;$('my\_form.submit();” %\>  
\<%= hidden\_field\_tag “form\_action”, “”, :id =\> “form\_action” %\>  
\<%= end\_form\_tag %\<
