How to Send Submit Button Value When Form Is Posted
In your provided code, the issue lies in obtaining the submit button's value. The button names are not set to "submit," which prevents PHP from populating the $_POST['submit'] variable. As a result, the isset($_POST['submit']) condition evaluates to false.
To resolve this, follow these steps:
1. Add a Hidden Input for Form Action:
Add a hidden input field with a name attribute of "action" and a value attribute of "submit" to your form. This will ensure that PHP knows the form has been submitted even if no specific submit button is pressed.
<input type="hidden" name="action" value="submit" />
2. Rename Button Names to "submit":
Change the name attribute of all submit buttons to "submit." This will enable PHP to retrieve the name of the button that was clicked.
<input>
3. Check for Submit Action in PHP:
In your PHP script, use the following code to check if the form has been submitted and which submit button was clicked:
if (isset($_POST['action'])) { echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />'; }
With these modifications, when the form is submitted, the name of the submit button that was clicked will be available in $_POST['submit'], allowing you to process the submitted data accordingly.
The above is the detailed content of How to Retrieve the Submit Button Value When a Form Is Posted?. For more information, please follow other related articles on the PHP Chinese website!