Two submit buttons in one form

前端 未结 19 2047
不思量自难忘°
不思量自难忘° 2020-11-21 23:23

I have two submit buttons in a form. How do I determine which one was hit serverside?

相关标签:
19条回答
  • 2020-11-22 00:10

    If you give each one a name, the clicked one will be sent through as any other input.

    <input type="submit" name="button_1" value="Click me">
    
    0 讨论(0)
  • 2020-11-22 00:10

    You can also use a href attribute and send a get with the value appended for each button. But the form wouldn't be required then

    href="/SubmitForm?action=delete"
    href="/SubmitForm?action=save"
    
    0 讨论(0)
  • 2020-11-22 00:11

    There’s a new HTML5 approach to this, the formaction attribute:

    <button type="submit" formaction="/action_one">First action</button>
    <button type="submit" formaction="/action_two">Second action</button>
    

    Apparently this does not work in IE9 and earlier, but for other browsers you should be fine (see: w3schools.com HTML <button> formaction Attribute).

    Personally, I generally use Javascript to submit forms remotely (for faster perceived feedback) with this approach as backup. Between the two, the only people not covered are IE<9 with Javascript disabled.

    Of course, this may be inappropriate if you’re basically taking the same action server-side regardless of which button was pushed, but often if there are two user-side actions available then they will map to two server-side actions as well.

    Edit: As noted by Pascal_dher in the comments, this attribute is also available on the <input> tag as well.

    0 讨论(0)
  • 2020-11-22 00:12

    Define name as array.

    <form action='' method=POST>
        (...) some input fields (...)
        <input type=submit name=submit[save] value=Save>
        <input type=submit name=submit[delete] value=Delete>
    </form>
    

    Example server code (PHP):

    if (isset($_POST["submit"])) {
        $sub = $_POST["submit"];
    
        if (isset($sub["save"])) {
            // save something;
        } elseif (isset($sub["delete"])) {
            // delete something
        }
    }
    

    elseif very important, because both will be parsed if not. Enjoy.

    0 讨论(0)
  • 2020-11-22 00:13

    Solution 1:
    Give each input a different value and keep the same name:

    <input type="submit" name="action" value="Update" />
    <input type="submit" name="action" value="Delete" />
    

    Then in the code check to see which was triggered:

    if ($_POST['action'] == 'Update') {
        //action for update here
    } else if ($_POST['action'] == 'Delete') {
        //action for delete
    } else {
        //invalid action!
    }
    

    The problem with that is you tie your logic to the user-visible text within the input.


    Solution 2:
    Give each one a unique name and check the $_POST for the existence of that input:

    <input type="submit" name="update_button" value="Update" />
    <input type="submit" name="delete_button" value="Delete" />
    

    And in the code:

    if (isset($_POST['update_button'])) {
        //update action
    } else if (isset($_POST['delete_button'])) {
        //delete action
    } else {
        //no button pressed
    }
    
    0 讨论(0)
  • 2020-11-22 00:17

    You can also do it like this (I think it's very convenient if you have N inputs).

    <input type="submit" name="row[456]" value="something">
    <input type="submit" name="row[123]" value="something">
    <input type="submit" name="row[789]" value="something">
    

    A common use case would be using different ids from a database for each button, so you could later know in the server wich row was clicked.

    In the server side (PHP in this example) you can read "row" as an array to get the id. $_POST['row'] will be an array with just one element, in the form [ id => value ] (for example: [ '123' => 'something' ]).

    So, in order to get the clicked id, you do:

    $index = key($_POST['row']);
    

    http://php.net/manual/en/function.key.php

    0 讨论(0)
提交回复
热议问题