![](/img/trans.png)
[英]Populate another select dropdown from database based on dropdown selection
[英]Populate dropdown based on another dropdown via Database
我嘗試使用JavaScript填充另一個基於下拉列表的下拉列表,例如:我有2個Select標簽
<select name="Dept" size="1"
onchange="setOptions(document.myform.Dept.options[document.myform.Dept.selectedIndex].value);">
<option value=" " selected="selected"> </option>
<option value="1">Web Developers</option>
<option value="2">Programmers</option>
<option value="3">another one</option>
</select><br> <br>
<select name="opttwo" size="1">
<option value=" " selected="selected">Please select one of the Department above first</option>
</select>
因此,當我選擇一個部門(例如,Web開發人員)時,我從包含學生姓名的表(Web)中獲取數據。
我有以下代碼:
function setOptions(chosen) {
var selbox = document.myform.opttwo
var list2 = document.form1.students
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select one of the Department above first',' ');
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new
Option('<?php
include 'config.php';
mysql_select_db("dept_db",$con);
$result=mysql_query("SELECT * from web");
while($row=mysql_fetch_array($result))
{
echo $row['web_student_name'] ;
}
?>');
}
if (chosen == "2") {
selbox.options[selbox.options.length] = new
Option('<?php
include 'config.php';
mysql_select_db("dept_db",$con);
$result=mysql_query("SELECT * from prog");
while($row=mysql_fetch_array($result))
{
echo $row['prog_student_name'] ;
}
?>');
}
if (chosen == "3") {
selbox.options[selbox.options.length] = new
Option('<?php
include 'config.php';
mysql_select_db("dept_db",$con);
$result=mysql_query("SELECT * from another");
while($row=mysql_fetch_array($result))
{
echo $row['another_student_name'] ;
}
?>');
}
}
</script>
它可以正常工作,但是當從表中獲取名稱時,所有彼此的名稱(eg, JosephStevePaul)
我想分別顯示每個人的名字
(e.g
Joseph
Steve
Paul
)
您也已經在php循環中添加了每個選項:
<?php
$result=mysql_query("SELECT * from prog");
while($row=mysql_fetch_array($result))
{
?>
selbox.options[selbox.options.length] = new Option('<?php echo $row['prog_student_name']; ?>');<?php
}
?>
對其他循環也這樣做。 此外,將此代碼添加到代碼的開頭。 您只需要一次。
<?php
include 'config.php';
mysql_select_db("dept_db",$con);
?>
您可以使用ajax實現此目的。 您可以根據第一個下拉列表中顯示的項目填充第二個下拉列表。 碼
<select name="Dept" size="1"
onchange="populatelist" id="dept">
<option value="0" selected="selected"> </option>
<option value="1">Web Developers</option>
<option value="2">Programmers</option>
<option value="3">another one</option>
</select>
當用戶選擇一個值時,例如:Web Developer將該值發送到ajax頁面,並從數據庫中查詢屬於該類別的所有學生列表。
function populatelist()
{
var qry=document.getelementbyid('dept').value;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
});
}
}
xmlhttp.open("POST", "pages/page.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("qry="+qry);
}
在page.php中寫查詢以獲取學生列表$ value = $ _ POST ['qry']; 並在其中寫入查詢,並根據查詢寫入的結果填充下拉列表。 並獲得結果並將其替換為ajax頁面中的響應,就這么簡單。
假設您的prog_student_name
是這樣的:
Joseph Steve Paul
我的意思是:
Option('<?php
include 'config.php';
mysql_select_db("dept_db",$con);
$result=mysql_query("SELECT * from prog");
$row=mysql_fetch_array($result);
$stu=explode("",$row['prog_student_name']) ;
for($i=0;$i<count($stu);$i++)
{
echo $stu[$i];
}
?>');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.