How can I tell which submit button was clicked
22,900
Solution 1
if request.getParameter("button-name") is not null then this is the button that was pressed
Solution 2
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
Solution 3
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')">
Comments
-
crauscher almost 2 years
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?
-
Muhammad Ahmad Afzal about 15 yearsare they <button> tags or <input type="submit"> ?
-
mkoryak about 15 yearsdo you care about this in the controller on in javascript?
-
Muhammad Ahmad Afzal about 15 yearswell IE6 sends a value for all the <button> tags, whether clicked or not
-
-
crauscher about 15 yearsNo, it does not appear in the query string.
-
BalusC over 12 yearsWarning: MSIE sends the button's body "some text" or "some other text" as parameter value, not the
value
attribute! You should not give all buttons the same name, but a different name and intercept on the presence of the button name only. I.e. if it is not null, then it is pressed, else if it is null, then it is not pressed.