您好我正在尝试构建一个允许用户选择多个值的下拉菜单,我从数据库查询2个值的id和名称,我使用id作为值,并使用名称作为描述选项。但是,下拉菜单只允许我一次选择一个选项。如何允许选择多个值以及如何处理发布的多个值?
print"<select name='Category' id='nodes' size='4' multiple='multiple'>\n";
$db = adodbConnect();
$query = "Select * From nodes";
$result = $db -> Execute($query);
while($row=$result->FetchRow())
{
$cat = $row['id'];
$desc =$row['name'];
print "<option value='$cat'>$desc</option>\n";
}
print "</select>\n";
答案 0 :(得分:0)
$db = adodbConnect();
$query = "Select * From nodes";
$result = $db -> Execute($query);
$select='';
$select.="<select name='Category' id='nodes' size='4' multiple='multiple'>\n";
while($row=$result->FetchRow())
{
$cat = $row['id'];
$desc =$row['name'];
$select.="<option value='$cat'>$desc</option>\n";
}
$select.="</select>\n";
echo $select;
在帖子中,您将获得数组形式的值,就像您选择两个选项一样
而不是array('1','2')
答案 1 :(得分:0)
你可以在php中处理多个值 -
<?php
header("Content-Type: text/plain");
foreach ($_GET['category'] as $selectedOption)
echo $selectedOption."<br />";
?>
答案 2 :(得分:0)
要以这种方式选择多个值,您只需使用Cmd(Mac)或Ctrl(Windows)键并单击多个。
处理帖子:
<?php
$categories = $_POST['category'];
foreach($categories as $category){
//do stuff
}
?>
答案 3 :(得分:0)
只需尝试使用以下内容:
<?php
$db = adodbConnect();
$query = "Select * From nodes";
$result = $db -> Execute($query);
print "<select size=\"4\" multiple=\"multiple\" name=\"Category[]\">";
print "<option value='-' >Select from list</option>";
while ($row=$result->FetchRow()){
$cat = $row['id'];
$desc = $row['name'];
print "<option value=$cat>$desc</option>";
}
print "</select>";
?>
我认为这可以帮助您解决问题。