Home >Backend Development >PHP Tutorial >How Can I Send the Value of a Submit Button on Form Post in PHP?

How Can I Send the Value of a Submit Button on Form Post in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-13 08:53:02410browse

How Can I Send the Value of a Submit Button on Form Post in PHP?

Sending Submit Button Values on Form Post

In this scenario, you aim to send the value of the submit button when a form is posted. However, you're unable to retrieve the submit button value in the PHP script ('buy.php').

The Problem:

The issue lies in the button names. In your code, the button names are not set to 'submit.' Consequently, the PHP variable $_POST['submit'] remains unset, resulting in isset($_POST['submit']) evaluating to false.

The Solution:

To rectify this, you need to ensure that the button names are explicitly set to 'submit.' Additionally, you can add a hidden input field to the form to specify the action being performed. Here's the revised code:

Sending Page:

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

Receiving Page: buy.php

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

By implementing these changes, you can effectively send the submit button's value when the form is posted and access it in the PHP script as $_POST['submit'].

The above is the detailed content of How Can I Send the Value of a Submit Button on Form Post in PHP?. 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