php 如何在php中获取选择框的多个选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407284/
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
How to get multiple selected values of select box in php?
提问by Param-Ganak
I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
我有一个 html 表单,它有一个选择列表框,您可以从中选择多个值,因为它的 multiple 属性设置为 multiple。考虑表单方法是“GET”。表单的html代码如下:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[]array.
我想在 display.php 页面上的选择列表框中显示选定的值。那么如何使用$_GET[]数组访问 display.php 页面上的选定值。
回答by Alex Jasmin
If you want PHP to treat $_GET['select2']as an array of options just add square brackets to the name of the select elementlike this: <select name="select2[]" multiple …
如果您希望 PHP 将其$_GET['select2']视为一组选项,只需在select 元素的名称中添加方括号,如下所示:<select name="select2[]" multiple …
Then you can acces the array in your PHP script
然后你可以访问你的 PHP 脚本中的数组
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GETmay be substituted by $_POSTdepending on the <form method="…"value.
$_GET可以替换为$_POST取决于<form method="…"值。
回答by Coufu
Change:
改变:
<select name="select2" ...
To:
到:
<select name="select2[]" ...
回答by rekha_sri
Use the following program for select the multiple values from select box.
使用以下程序从选择框中选择多个值。
multi.php
多.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
值.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
回答by Abdul Kalam Azad
You can use this code to retrieve values from multiple select combo box
您可以使用此代码从多个选择组合框中检索值
HTML:
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
回答by Vivek
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
你可以像这样直接迭代它
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
或者你可以这样做
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
回答by Rynika
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
回答by ahmed
This will display the selected values:
这将显示选定的值:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
回答by Dulith De Costa
You could do like this too. It worked out for me.
你也可以这样做。它对我有用。
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHPcode below. It print the selected multiple values accordingly.
然后从下面的PHP代码中进行多项选择。它相应地打印选定的多个值。
$shift=$_POST['selectDuration'];
print_r($shift);
回答by SwR
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
回答by Drako
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
我用 javascript + HTML 解决了我的问题。首先,我检查选定的选项并将其保存在表单的隐藏字段中:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-) I hope it'll be work for somebody more. Thanks to all.
接下来,我通过发布该字段并获取所有字符串;-) 我希望它对更多人有用。谢谢大家。

