Home > Backend Development > PHP Tutorial > How to Send Form Data with Button Values in PHP?

How to Send Form Data with Button Values in PHP?

Mary-Kate Olsen
Release: 2024-11-16 06:46:02
Original
835 people have browsed it

How to Send Form Data with Button Values in PHP?

Sending Form Data with Button Value

You're facing an issue with submitting the value of submit buttons in a form post. In your provided code, the button names are labeled as "submit" instead of the intended product names, which prevents the PHP script from retrieving the desired value.

Solution:

To resolve this issue, you'll need to assign unique names to the submit buttons and ensure that the PHP script expects the value in the correct variable name. Here's an updated code snippet:

Sending Page:

<html>
<form action="buy.php" method="post">
    <select name="name">
        <option>John</option>
        <option>Henry</option>
    <select>
    <input type="hidden" name="action" value="submit">
    <input type="submit" name="submit" value="Tea">
    <input type="submit" name="submit" value="Coffee">
</form>
</html>
Copy after login

Receiving Page (buy.php):

<?php
if (isset($_POST['action'])) {
    $name = $_POST['name'];
    $purchase = $_POST['submit'];
    // Database operations here
}
?>
Copy after login

In this updated code:

  • We've added a hidden input with the name "action" and the value "submit". This ensures that PHP can identify when a form submission has occurred.
  • The submit buttons now have unique names, allowing PHP to retrieve the correct value from the $_POST array.
  • In the receiving PHP script, we check for the existence of the "action" field to confirm the form submission.

By making these changes, you'll be able to successfully send the values of your submit buttons along with the other form data.

The above is the detailed content of How to Send Form Data with Button Values in PHP?. 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