crauscher
crauscher

Reputation: 6618

How can I tell which submit button was clicked

I have several different submit buttons on my JSP in one form tag that all point to the same servlet. I need to know which submit button was clicked. How can I find out which button was clicked?

Upvotes: 8

Views: 23387

Answers (4)

The Surrican
The Surrican

Reputation: 29866

<button type="submit" name="somename" value="button1">some text</button>
<button type="submit" name="somename" value="button2">some other text</button>

you will have the post variable "somename" set to the according value, no matter the dispalyed value.

Upvotes: 0

Gary Kephart
Gary Kephart

Reputation: 4994

This is kind of similar to the DispatchAction in Struts. What they do is to have a hidden field, and when you submit the form, have onClick() set the value to specify which action is taken.

<input type="hidden" name="dispatchAction"/>
<input type="submit" value="Edit"   onClick="setDispatchAction('edit')">
<input type="submit" value="Delete" onClick="setDispatchAction('delete')">

Upvotes: 1

Maurice Perry
Maurice Perry

Reputation: 32831

if request.getParameter("button-name") is not null then this is the button that was pressed

Upvotes: 12

matt b
matt b

Reputation: 139931

Each Submit button should have a different name:

<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">

Then, the name of the input should appear in the parameters sent to wherever the form is posting to, something like

post.jsp?key=value&submit3=&....

http://www.w3schools.com/tags/tag_input.asp

Upvotes: 4

Related Questions