1

Currently I am working on a HTML page where I can upload an Excel and also get data from database between a specified date range. One submit button would bring out data from excel and database and compare value from both list and shows up the conflicting values. I need to add another submit button where the user can set how the mapping must be done for the conflicting values in the same form.

How to use two submit buttons simultaneously with data posted in first submit button accessed in second submit button in PHP?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hema v
  • 11
  • 1
  • 2

2 Answers2

0

As long as the submit buttons are placed inside the same form element, clicking on any of them will submit the form they are placed in.

With that, all fields in the same form will be submitted.

However, you probably want to know which submit button was clicked to do things differently.

You can add name property to specify which submit button was clicked.

<input type="submit" name="submit1">
<input type="submit" name="submit2">

Assuming this form is submitted with POST method, you can check which submit button was clicked using the example below.

$importData = isset($_POST["submit1"]);
$mapSettings = isset($_POST["submit2"]);

So, you can achieve this by placing all the fields in different forms into the same form element. All fields within the form element will be submitted despite of which submit button was clicked.

You can then use the boolean result above to determine what to do.

josephting
  • 2,617
  • 2
  • 30
  • 36
0

The best answer I have seen so far for this situation is:

<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 only problem with that is you tie your logic to the text within the input. You could also give each one a unique name and just 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
}

There is a nice post about this here: Two submit buttons in one form