发送提交按钮的值时,得到的形式发布 [英] Send value of submit button when form gets posted

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

问题描述

我有名称的列表,并与产品名一些按钮。当其中一个按钮被点击发送到一个PHP脚本列表的信息,但我不能点击提交按钮发送它的价值。它是如何做?
我煮了我的code到以下内容:

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 ['提交'] 值不组。正如使用isset($ _ POST ['提交'])计算结果为假。

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天全站免登陆