Home > Backend Development > PHP Tutorial > How to Capture Submit Button Value in a Form Submission?

How to Capture Submit Button Value in a Form Submission?

Mary-Kate Olsen
Release: 2024-11-14 18:32:02
Original
738 people have browsed it

How to Capture Submit Button Value in a Form Submission?

How to Send Submit Button Value When Form Gets Posted

In web development, it is common to have a form that sends user input to a server-side script for processing. When the user clicks on a submit button, the form data is sent along with the request. However, the submit button's value is often not included in the form data by default.

Problem:

Consider a code snippet that allows users to select a name from a drop-down list and then click on a button to purchase a product. The form is sent to the buy.php script, which processes the name field but does not include the value of the submit button.

Solution:

To send the submit button's value, we need to give it a name that is identifiable by the server-side script. In the modified HTML code provided:

<input>
Copy after login

We assign the name "submit" to both submit buttons. Now, in the PHP script:

<?php
if (isset($_POST['action'])) {
    echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Copy after login

We check if the "action" hidden input is set, which indicates that the form was submitted. If it is, we echo the value of the "submit" field, which represents the submit button that was clicked.

Note:

It's crucial to ensure that each HTML element has a unique ID and to give the submit buttons the same name to capture the correct value on the server side.

The above is the detailed content of How to Capture Submit Button Value in a Form Submission?. 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