douqingnao9246 2017-02-28 14:25
浏览 203

使用来自数据库的Select选项

I have two select on my form the first get all its elements from a table named "department" in my school database.

It is working no problem, but my question is how to make the second select option box receive elements coming from the table "class" having column "departmentid" which is equal to the selected department in the first select option I mean if the user selects department "IT" the second select option may bring classes"4,5,6" because they were registered with the ID of that department.

The reason i am doing this is because all the departments don't have the same classes, It would be great if this event occured after clicking on the first select option.

My Database name is "School" Table1 "departments" with its columns "Departmentid,Name" Table2 "Class" with its columns "classid,level,Departmentid"

//First Select Option
<select class="form-control">                                         
<option selected="selected">Show All Departments</option>
<?php
$link = mysqli_connect("localhost", "root", "", "school");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());}
$con="SELECT * FROM departments";
$query_test = mysqli_query($link,$con) or die ("MySQL error: " .mysqli_error($link) ." Query: $query");
while($row=mysqli_fetch_array($query_test))
{
$result=$row['Name'];
$results=$row['Departmentid'];
?>
<option name="find"value="<?php echo $row['Name'];?>"><?php echo $row['Name'];?></option>
<?php
}
?>
</select>

//Second select option
<select class="form-control">
<option selected="selected">Show All Classes</option>
//Input those classes in this select option
<option> </Option>
</select>
  • 写回答

1条回答 默认 最新

  • doujiao1905 2017-02-28 14:41
    关注

    AJAX is the way to go.

    • On departement change call AJAX function with departement ID as data.
    • Make a query to get all classes of departement and return result.
    • Update classes options with those you get using Javascript.
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?