php 使用 $_POST 从 HTML 中获取选择选项值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17139501/
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
Using $_POST to get select option value from HTML
提问by Felicia Tan
I use select
as below:
我使用select
如下:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select
option and store it into a variable for future use, in PHP?
如何从select
选项中获取值并将其存储到变量中以备将来使用,在 PHP 中?
回答by Praveen Kumar Purushothaman
Use this way:
使用这种方式:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option>
tags.
但是,为您的<option>
标签赋予价值总是更好。
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
回答by Ryan
You can access values in the $_POST
array by their key. $_POST is an associative array, so to access taskOption
you would use $_POST['taskOption'];
.
您可以$_POST
通过键访问数组中的值。$_POST 是一个关联数组,因此要访问taskOption
您将使用$_POST['taskOption'];
.
Make sure to check if it exists in the $_POST array before proceeding though.
在继续之前,请确保检查它是否存在于 $_POST 数组中。
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
进程.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}
回答by Dan Costinel
You can do it like this, too:
你也可以这样做:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
回答by Jeroen Vannevel
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];
回答by user2465080
Depends on if the form that the select is contained in has the method set to "get" or "post".
取决于包含选择的表单是否将方法设置为“get”或“post”。
If <form method="get">
then the value of the select will be located in the super global array $_GET['taskOption']
.
如果<form method="get">
那么选择的值将位于超级全局数组中$_GET['taskOption']
。
If <form method="post">
then the value of the select will be located in the super global array $_POST['taskOption']
.
如果<form method="post">
那么选择的值将位于超级全局数组中$_POST['taskOption']
。
To store it into a variable you would:
要将其存储到变量中,您将:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
获取更多信息的好地方是 PHP 手册:http: //php.net/manual/en/tutorial.forms.php
回答by Jaime Gris
Like this:
像这样:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST
array is always based on the value of the name
attribute of any HTML input.
$_POST
数组的索引始终基于name
任何 HTML 输入的属性值。
回答by Bulbul Bigboss
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
尝试这个
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>
回答by Lucky
-- html file --
-- html 文件 --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
-- php 文件 --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);