Home > Article > Backend Development > How to Retrieve the Value of a Submitted Button in a Form with Multiple Buttons?
In web development, when a user interacts with a form by clicking a button or submitting data, the form's contents are sent to a server-side script for processing. However, retrieving the value of the submitted button can be challenging.
Problem Statement
You have created a form with a list of names and product buttons. When one of the buttons is clicked, the information from the list should be sent to a PHP script. However, the submit button value is not being sent.
Solution
To successfully send the value of the submit button:
Rename Button Names: Replace "submit" in the button attributes with unique names, as PHP will not recognize "submit" when multiple buttons share the same name. For example:
<input>
Add a Hidden Input: Include a hidden input field with the name "action" and a value of "submit" to ensure that the server-side script knows that the form was submitted.
<input type="hidden" name="action" value="submit">
Check PHP $_POST['action']: In the receiving PHP script, use the isset() function to check if the "action" value is set. If it is, the form was submitted and you can access the submitted button value:
if (isset($_POST['action'])) { echo "<br />The " . $_POST['submit'] . " submit button was pressed</br>"; }
By implementing these changes, you ensure that the submit button value is correctly sent and processed in your PHP script.
The above is the detailed content of How to Retrieve the Value of a Submitted Button in a Form with Multiple Buttons?. For more information, please follow other related articles on the PHP Chinese website!