如何在php中获得多个选择框的选择值?

我有一个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>&nbsp;</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>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>

我想在display.php页面的选择列表框中显示所选值。那么如何使用$_GET[]数组访问display.php页面上的选定值呢?

806770 次浏览

如果你想让PHP将$_GET['select2']视为一个选项数组,只需在选择元素的名称后面加上方括号,就像这样:<select name="select2[]" multiple …

然后可以在PHP脚本中访问该数组

<?php
header("Content-Type: text/plain");


foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";

根据<form method="…"的值,$_GET可以被$_POST取代。

变化:

<select name="select2" ...

:

<select name="select2[]" ...

使用下面的程序从选择框中选择多个值。

multi.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
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}


?>

您可以使用此代码从多个选择组合框中检索值

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
$values = $_POST['ary'];


foreach ($values as $a){
echo $a;
}
?>

这将显示所选的值:

<?php


if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}


?>

我用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;

接下来,我通过post该字段和获得所有的字符串;-) 我希望这是给别人的工作。

// 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");
}
...


...
}
?>
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
    <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>&nbsp;</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>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>

你可以像这样直接迭代

foreach ($_GET['select2'] as $value)
echo $value."\n";

或者你可以这样做

$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";

你也可以这样做。这对我来说很有效。

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

然后从下面的PHP代码中进行多重选择。它相应地打印所选的多个值。

$shift=$_POST['selectDuration'];


print_r($shift);