我有一个名称列表和一些带有产品名称的按钮。单击其中一个按钮时,列表信息将发送到 PHP 脚本,但我无法点击提交按钮发送其值。它是如何完成的? 我将我的代码归结为以下内容:
发送页面:
<html>
<form action="buy.php" method="post">
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<input id='submit' type='submit' name='Tea' value='Tea'>
<input id='submit' type='submit' name='Coffee' value='Coffee'>
</form>
</html>
接收页面:buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some SQL database magic happens
?>
除了发送提交按钮值之外的一切都完美无缺。
最佳答案
按钮名称未提交,因此未设置 php $_POST['submit']
值。如 isset($_POST['submit'])
计算结果为 false。
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
关于php - 发布表单时发送提交按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22579616/