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

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

?>

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


潇潇雨雨
浏览 370回答 3
3回答

明月笑刀无情

按钮名称未提交,因此$_POST['submit']未设置php 值。如in isset($_POST['submit'])评估为false。<html><form action="" method="post">&nbsp; &nbsp; <input type="hidden" name="action" value="submit" />&nbsp; &nbsp; <select name="name">&nbsp; &nbsp; &nbsp; &nbsp; <option>John</option>&nbsp; &nbsp; &nbsp; &nbsp; <option>Henry</option>&nbsp; &nbsp; <select><!--&nbsp;make sure all html elements that have an ID are unique and name the buttons submit&nbsp;-->&nbsp; &nbsp; <input id="tea-submit" type="submit" name="submit" value="Tea">&nbsp; &nbsp; <input id="coffee-submit" type="submit" name="submit" value="Coffee"></form></html><?phpif (isset($_POST['action'])) {&nbsp; &nbsp; echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';}?>

缥缈止盈

使用此代替:<input id='tea-submit' type='submit' name = 'submit'&nbsp; &nbsp; value = 'Tea'><input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>

qq_笑_17

就像其他人说的那样,您可能会误解了唯一ID的想法。我要补充的是,我不喜欢在这里使用“值”作为标识属性的想法,因为它可能随着时间而改变(即,如果您想提供多种语言)。<input id='submit_tea'&nbsp; &nbsp; type='submit' name = 'submit_tea'&nbsp; &nbsp; value = 'Tea' /><input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />并在您的PHP脚本中if( array_key_exists( 'submit_tea', $_POST ) ){&nbsp; // handle tea}if( array_key_exists( 'submit_coffee', $_POST ) ){&nbsp; // handle coffee}另外,您可以添加类似的内容,例如if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )是否要检查数据是否正确发布。
打开App,查看更多内容
随时随地看视频慕课网APP