php 表单发布时发送提交按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22579616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send value of submit button when form gets posted
提问by M.G.Ptheitroadot
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:
我有一个名称列表和一些带有产品名称的按钮。当单击其中一个按钮时,列表的信息将发送到 PHP 脚本,但我无法点击提交按钮来发送其值。它是如何完成的?我将我的代码归结为以下内容:
The sending page:
发送页面:
<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>
The receiving page: buy.php
接收页面:buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some database magic happens
?>
Everything except sending the submit button value works flawlessly.
除了发送提交按钮值之外,一切都完美无缺。
回答by robbmj
The button names are not submit, so the php $_POST['submit']
value is not set. As in isset($_POST['submit'])
evaluates to false.
按钮名称未提交,因此$_POST['submit']
未设置php值。正如在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 />';
}
?>
回答by user4035
Use this instead:
改用这个:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
回答by developers-have-no-vacation
To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.
首先,两次使用相同的 ID 并不是一个好主意。ID 应该是唯一的,如果您需要为元素设置样式,则应该使用类来应用 CSS。
At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().
最后,您将提交按钮的名称定义为 Tea and Coffee,但在您的 PHP 中,您使用提交作为索引。例如,您的索引应该是 $_POST['Tea']。这将需要您检查它是否被设置,因为它只发送一个,您可以使用 isset() 来做到这一点。
Buy anyway , user4035 just beat me to it , his code will "fix" this for you.
无论如何都买,user4035 只是打败了我,他的代码会为你“解决”这个问题。
回答by phn
The initial post mentioned buttons. You can also replace the input tags with buttons.
最初的帖子提到了按钮。您还可以用按钮替换输入标签。
<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>
The name
and value
attributes are required to submit the value when the form is submitted (the id
attribute is not necessary in this case). The attribute type=submit
specifies that clicking on this button causes the form to be submitted.
将name
和value
属性须提交时提交表单(该值id
属性是没有必要在这种情况下)。该属性type=submit
指定单击此按钮会导致提交表单。
When the server is handling the submitted form, $_POST['product']
will contain the value "Tea" or "Coffee" depending on which button was clicked.
当服务器处理提交的表单时,$_POST['product']
将根据单击的按钮包含值“Tea”或“Coffee”。
If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).
如果您愿意,您还可以要求用户在提交表单之前进行确认(例如,在您实施删除按钮时很有用)。
<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>
回答by DrPepperJo
Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).
就像其他人说的那样,您可能误解了唯一 ID 的概念。我要补充的是,我不喜欢在这里使用“值”作为标识属性的想法,因为它可能会随着时间的推移而改变(即,如果您想提供多种语言)。
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
and in your php script
并在您的 php 脚本中
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )
if you want to check if data was acctually posted.
此外,您可以添加一些内容,例如if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )
是否要检查数据是否已实际发布。
回答by Aldo
You could use something like this to give your button a value:
你可以使用这样的东西给你的按钮一个值:
<?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>
This will give you the string "SomeValue" as a result
这将为您提供字符串“SomeValue”作为结果
回答by John
You should also put a forward slash in the <select>
end tag.
您还应该在<select>
结束标记中放置一个正斜杠。
<select name="name">
<option>John</option>
<option>Henry</option>
</select>
回答by Orchid Engr
You can maintain your html as it is but use this php code
您可以按原样维护 html,但请使用此 php 代码
<?php
$name = $_POST['name'];
$purchase1 = $_POST['Tea'];
$purchase2 =$_POST['Coffee'];
?>