# How to use multiple buttons on the same form with AJAX?

**URL:** <https://www.ruby-forum.com/t/how-to-use-multiple-buttons-on-the-same-form-with-ajax/58191>\
**Category:** Rails\
**Created:** [April 19, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-use-multiple-buttons-on-the-same-form-with-ajax/58191 "2006-04-19T15:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cnick](https://www.ruby-forum.com/u/cnick)\
**Post date:** [April 19, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-use-multiple-buttons-on-the-same-form-with-ajax/58191/1 "2006-04-19T15:29:47Z")

</div>

Hi,

We are trying to use multiple submit buttons in the same form to tie to  
different actions on the server side.

For example:

This works with non-AJAX type requests. However, now that we are trying  
to  
do the same with AJAX, it is not working. Whichever button we press, we  
get  
the value of the first button on the form being passed as a parameter to  
the  
server. It looks like to me the problem might be related to how Rails  
generates its buttons with the same name (commit) but different values.  
Is  
this legal in AJAX? Does AJAX use DOM? Is there a work-around for this?

The parameters hash looks like…

“commit” =\> “edit”

even if we press the “cancel” or “test configuration” buttons.

I have Googled about but haven’t found any answers. Can anyone point me  
in  
the right direction or recommend a good book on AJAX?

Any help would be appreciated. Thanks,

cn

---

<div class="post-metadata">

**Author:** ![cnick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cnick](https://www.ruby-forum.com/u/cnick)\
**Post date:** [April 19, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-use-multiple-buttons-on-the-same-form-with-ajax/58191/2 "2006-04-19T16:00:57Z")

</div>

Chris,

If I recall, this is a known issue with XHR and you just can’t.

Ed

---

<div class="post-metadata">

**Author:** ![cnick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cnick](https://www.ruby-forum.com/u/cnick)\
**Post date:** [April 19, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/how-to-use-multiple-buttons-on-the-same-form-with-ajax/58191/3 "2006-04-19T16:08:15Z")

</div>

Edward F. wrote:

> Chris,
> 
> If I recall, this is a known issue with XHR and you just can’t.
> 
> Ed

def onclick\_button\_to( label, \*options)  
button\_to\_function label, %Q{location.href=’#{url\_for \*options}’}  
end

This is in my helper and what I use for my cancel buttons in my  
multi-button forms. You pass the same parameters as you would for  
link\_to. In my view it looks like…

\<%= form\_tag :action =\> “save” %\>  
\<%= onclick\_button\_to “cancel”, :action =\> “index” %\>  
\<%= submit\_tag ‘save’ %\>  
\<%= end\_form\_tag %\>

This is non-ajax usage but you would just need to modify the javascript  
in the onclick\_button\_to method to get what you wanted…
