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!