I have two submit buttons in a form. How do I determine which one was hit serverside?
yes, multiple submit buttons can include in the html form. One simple example is given below.
Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.
Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.
No, a form has only one action.
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 }
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With