File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Multiple buttons in a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Multiple buttons in a form" Watch "Multiple buttons in a form" New topic

Multiple buttons in a form

Bianca Deloso

Joined: Sep 23, 2003
Posts: 15
Hi guys,
My page has 4 buttons in one form. One is the submit button, another one is a cancel button, and the other two are buttons that does another action if chosen. (For example, delete and edit).
I use <html:img> or <html:image> tags for the buttons. I was wondering how I could get my action to determine which button was clicked and have the forward subsequently use the right target to forward to.
Any help would be great. Thanks!
Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
use javascript for determine the wich button is chk
td height="25" align="left">
<a href="javascript:saveRecord()">
<img src="images/ttl_set.gif" name='submit1' border="0" align="absMiddle"/>
Bianca Deloso

Joined: Sep 23, 2003
Posts: 15
I tried out the code but I think I did something wrong. I added two of my buttons with the javascript but when I click on one of them I also pass through the 'action/method' of the other button.
Incidentally, using this means placing the name of the method of a single action in the javascript right?
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
Hi Bianca,
One way you could do this is to put a property on your action form that is set by a javascript function to identify which button was pressed by the user. So in your action form, add a property called, say, "buttonPressed" or something like that. Create a javascript function that takes the button identifier as an arg, then call this in the onclick property of the button passing the button id in as the arg. The function sets the buttonPressed property, then submits the form, something like this:

You can then put conditional logic in your action to determine the proper forward.
A disclaimer: This assumes that all the buttons are calling one action, the action declared in the form tag.

My theory of evolution is that Darwin was adopted. - Steven Wright
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4444

Seethis tip from Ted Husted.

Junilu - [How to Ask Questions] [How to Answer Questions]
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
Oops, sorry Bianca, I should have read your post better, I missed the part about the image buttons.
David Yutzy
tumbleweed and gunslinger
Ranch Hand

Joined: Jun 29, 2001
Posts: 192
I use DispatchAction along with the "method" hidden field to determine which method is called. I can place any number of buttons, images, buttons, etc.
So in the onclick of the button, use this:
Then, as an example of a DispatchAction class is as follows:
I agree. Here's the link:
subject: Multiple buttons in a form
Similar Threads
html:image - running into an infinite loop
how to decide which button is submitted in an action class
want help in iframe
Struts Cancel Button submits the form
use action without actionMapping