Two submit buttons in one form

前端 未结 19 2080
不思量自难忘°
不思量自难忘° 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:13

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

    
    
    

    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:

    
    
    

    And in the code:

    if (isset($_POST['update_button'])) {
        //update action
    } else if (isset($_POST['delete_button'])) {
        //delete action
    } else {
        //no button pressed
    }
    

提交回复
热议问题