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

时间:2014-03-22 15:30:56

标签: php html forms button post

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

除了发送提交按钮值之外的所有内容都可以正常运行。

8 个答案:

答案 0 :(得分:43)

按钮名称未提交,因此未设置php $_POST['submit']值。正如在isset($_POST['submit'])评估为假。

<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 />';
}
?>

答案 1 :(得分:14)

请改用:

<input id='tea-submit' type='submit' name = 'submit'    value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>

答案 2 :(得分:5)

首先,使用相同的ID两次不是一个好主意。 ID应该是唯一的,如果您需要设置样式元素,则应该使用类来应用CSS。

最后,您将提交按钮的名称定义为Tea and Coffee,但在PHP中,您使用submit作为索引。你的索引应该是$ _POST [&#39; Tea&#39;]。这将要求你检查它是否设置,因为它只发送一个,你可以用isset()来做。

购买无论如何,user4035只是打败了我,他的代码将#34;修复&#34;这适合你。

答案 3 :(得分:3)

最初的帖子提到按钮。您也可以用按钮替换输入标签。

<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>

提交表单时提交值需要namevalue属性(在这种情况下不需要id属性)。属性type=submit指定单击此按钮会导致提交表单。

当服务器处理提交的表单时,$_POST['product']将包含值&#34; Tea&#34;或&#34;咖啡&#34;取决于点击了哪个按钮。

如果需要,您还可以在提交表单之前要求用户确认(例如,当您实现删除按钮时非常有用)。

<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>

答案 4 :(得分:2)

像其他人说的那样,你可能会错过理解一个独特身份的想法。我要补充的是,我不喜欢在这里使用“值”作为识别属性的想法,因为它可能会随着时间的推移而改变(即如果你想提供多种语言)。

<input id='submit_tea'    type='submit' name = 'submit_tea'    value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />

并在你的php脚本中

if( array_key_exists( 'submit_tea', $_POST ) )
{
  // handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
  // handle coffee
}

此外,如果要检查数据是否是主动发布的,您可以添加if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )之类的内容。

答案 5 :(得分:0)

你可以维护你的html,但是使用这个php代码

<?php
    $name = $_POST['name'];
    $purchase1 = $_POST['Tea'];
    $purchase2 =$_POST['Coffee'];
?>

答案 6 :(得分:0)

您可以使用类似的方法为按钮赋予一个值:

<?php
if (isset($_POST['submit'])) {
  $aSubmitVal = array_keys($_POST['submit'])[0];
  echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
  <input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>

这将为您提供字符串“ SomeValue”

https://i.imgur.com/28gr7Uy.gif

答案 7 :(得分:-2)

试试这段代码:

    <html>
    <form action="" method="post">
        <input type="hidden" name="action" value="submit" />
        <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>
<?php
if (isset($_POST['name']) && isset($_POST['tea'])){
$tea = $_POST['tea'];
$name = $_POST['name'];
$message .= "Button  : ".$tea."\n";
$message .= "Name    : ".$name."\n";
$send = "your_email@service.tld";
$subject = "Your Subject here";
$headers = "From:$name<email@local.com>";
mail($send,$subject,$message,$headers);
}
else if (isset($_POST['name']) && isset($_POST['coffee'])){
$coffee = $_POST['coffee'];
$name = $_POST['name'];
$message .= "Button  : ".$coffee."\n";
$message .= "Name    : ".$name."\n";
$send = "your_email@service.tld";
$subject = "Your Subject here";
$headers = "From:$name<email@local.com>";
mail($send,$subject,$message,$headers);
}
?>