表单发布时发送提交按钮的值 [英] Send value of submit button when form gets posted

查看:23
本文介绍了表单发布时发送提交按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名称列表和一些带有产品名称的按钮.当单击其中一个按钮时,列表的信息将发送到 PHP 脚本,但我无法点击提交按钮来发送其值.它是如何完成的?我将代码归结为以下内容:

I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done? I boiled my code down to the following:

发送页面:

<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
?>

除了发送提交按钮值之外,一切都完美无缺.

Everything except sending the submit button value works flawlessly.

推荐答案

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

The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to 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 />';
}
?>

这篇关于表单发布时发送提交按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆