繁体   English   中英

通过数据库基于另一个下拉列表填充下拉列表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM