I have a form. I want it to have two buttons (buttonA, buttonB). Is there a better way to figure out which button was clicked than keeping a hidden input field, and setting its value via javascript in onClick() to a different string for each?
This is what this article is demonstrating: http://www.java2s.com/Code/Java/JSP/JspformUsingButtons.htm
But it seems a little weird, is there no better way than jumping through those hoops?
Thanks
You can use the name and value attributes to differentiate the buttons on the form, then read the appropriate value from the server. Here is an example:
<form method="get" action="test.htm" target="new">
<button type="submit" name="button" value="1">Button One</button>
<button type="submit" name="button" value="2">Button Two</button>
</form>
When Button 1 is clicked, the URL will contain button=1, and of course when Button 2 is clicked, button=2.
Obviously, you can use any name property you wish, as long as both buttons have the same name. This will work the same using POST instead of GET.
http://jsfiddle.net/M74xN/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With