一尘不染

表单发布后发送提交按钮的值

html

我有一个名称列表和一些带有产品名称的按钮。当单击其中一个按钮时,列表信息将发送到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 database magic happens
?>

除了发送提交按钮值外,其他所有东西都可以正常工作。


阅读 240

收藏
2020-05-10

共1个答案

一尘不染

按钮名称未提交,因此$_POST['submit']未设置php 值。如in 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 />';
}
?>
2020-05-10