Determine which submit button was used?

Can I determine which submit button was used? I have a confirmation form with 2 submit buttons. The first will confirm the order, perform some database tasks, and then redirect. The second, which is the β€œCancel” button, simply redirects to the same page without performing any database tasks.

Is it possible in the servlet, preferably through the request object, to determine which submit button was used? I would prefer not to depend on Javascript, as it is quite simple, but resort to it if possible.

Thanks.

+4
source share
4 answers
<button name="someName" value="someValue" type="submit">Submit</button> <button name="otherName" value="otherValue" type="submit">Cancel</button> 

You will have someName=someValue or otherName=otherValue in your request data

+10
source

Of course, just give each of your submit buttons an attribute name , and depending on which one was clicked, it will appear in the presented variables:

 <input type="submit" name="doConfirm" value="Confirm" /> <input type="submit" name="doCancel" value="Cancel" /> 
+8
source

As already mentioned, two buttons with different names achieve your goal. However, there are some potential issues to be aware of when relying on this in your application. I think they are specific to Internet Explorer, so if you don't need to support older versions of IE, you can ignore it. Both include the behavior of the submitted form when the user presses the enter button when one of the form elements has focus. This article uses ASP to demonstrate problems, but the HTML side of things is important.

+1
source

When using multiple submit buttons, I like to use javascript to set the value of a hidden form field that describes the action that should take place.

For instance:

 <input type="hidden" name="action" id="form-action" /> <input type="submit" value="Save" onClick="document.getElementById('form-action').value='save'" /> <input type="submit" value="Copy" onClick="document.getElementById('form-action').value='copy'" /> 
0
source

All Articles