Home > Backend Development > PHP Tutorial > How to Retrieve the Submit Button Value When a Form Is Posted?

How to Retrieve the Submit Button Value When a Form Is Posted?

Patricia Arquette
Release: 2024-11-23 07:12:13
Original
786 people have browsed it

How to Retrieve the Submit Button Value When a Form Is Posted?

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" />
Copy after login

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>
Copy after login

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 />';
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template