One form, two buttons?

[亡魂溺海] 提交于 2021-02-04 19:48:11

问题


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


回答1:


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/




回答2:


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
            }
}



回答3:


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>



回答4:


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')


来源:https://stackoverflow.com/questions/10116052/one-form-two-buttons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!