user291701
user291701

Reputation: 39671

One form, two buttons?

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

Upvotes: 3

Views: 11257

Answers (4)

Fthi.a.Abadi
Fthi.a.Abadi

Reputation: 322

Although the answer might be late. This works perfectly. Use the following in your HTML.

<form action="/route" method="POST">
    <button name="submit" value="ONE">
    <button name="submit" value="TWO">
</form>

Then in your Python(Flask) use this

if request.form['submit'] == 'ONE':
    print('button one clicked')
elif request.form['submit'] == 'TWO':
    print('button two clicked')

Upvotes: 0

mykey
mykey

Reputation: 575

with jsp and servlets the possible operation will be:

in the html or jsp page

<form method="POST" action="servlet">
  <button type="submit" name="action" value="add">add</button>
  <button type="submit" name="action" value="edit">delete</button>
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​

Servlet POST method

if (request.getParameter("action") != null) //if action is not null
        {
            String action = request.getParameter("action");
            if (action.equals("add")) //add button clicked
            {
               //do your work here
            }
            else if (action.equals("edit")) //delete button clicked
            {
                //your work here
            }
}

Upvotes: 2

Nik
Nik

Reputation: 201

Create a form, set different values of submit button, and have your script perform different actions depending on what button is pressed.

if (isset($_POST) {
    if ($_POST['button'] == 1) {
     /* BUTTON ONE PRESSED, INSERT PROCESSING HERE*/
    } else if ($_POST['button'] == 2) {
     /* BUTTON TWO PRESSED, INSERT PROCESSING HERE*/
    } else {
     /* NO BUTTON PRESSED, (THIS ELSE STATEMENT MAY NOT BE NEEDED) */
    }

/** END OF PHP CODE **/
<--! start of html -->
<html>
<form method="get" action="<?php echo "$server['php_self']; ?>" target="new">
  <button type="submit" name="button" value="1">Button One</button>
  <button type="submit" name="button" value="2">Button Two</button>
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​
</html>

Upvotes: 0

Mike Christensen
Mike Christensen

Reputation: 91580

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/

Upvotes: 7

Related Questions