Home  >  Article  >  Backend Development  >  How to Retrieve the Value of a Submitted Button in a Form with Multiple Buttons?

How to Retrieve the Value of a Submitted Button in a Form with Multiple Buttons?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-12 22:52:02778browse

How to Retrieve the Value of a Submitted Button in a Form with Multiple Buttons?

Sending Submit Button Values in Form Postings

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:

  1. 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>
  2. 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">
  3. 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!

Statement:
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