Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One form, two buttons?

Tags:

html

forms

jsp

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

like image 399
user291701 Avatar asked Apr 25 '26 03:04

user291701


1 Answers

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/

like image 111
Mike Christensen Avatar answered Apr 27 '26 20:04

Mike Christensen