Skip to content Skip to sidebar Skip to footer

Determining Which Submit Button Was Used?

Is it possible to determine which submit button was used? I have a confirmation form with 2 submit buttons. The first would confirm the order, do some DB tasks, then redirect. T

Solution 1:

<button name="someName" value="someValue"type="submit">Submit</button>
<button name="otherName" value="otherValue"type="submit">Cancel</button>

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

Solution 2:

Sure, just give each of your submit buttons a name attribute, and whichever one was clicked will appear in the submitted variables:

<inputtype="submit" name="doConfirm" value="Confirm"  />
<inputtype="submit" name="doCancel"  value="Cancel"  />

Solution 3:

As others have mentioned having two buttons with different names will accomplish your goal. However there are some potential problems to be aware of when relying on this in your application. I think they are Internet Explorer specific, so if you don't need to support older versions of IE you might be able to disregard. Both involve the behavior of the submitted form when a user hits enter when one of the form's elements has focus. This article uses ASP to demonstrate the problems but the HTML side of things is relevant.

Solution 4:

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

For example:

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

Post a Comment for "Determining Which Submit Button Was Used?"