douyue7536 2014-04-06 14:33
浏览 22
已采纳

如何使用jQuery,PHP和MySQL动态填充依赖选择菜单?

I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.

I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").

I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.

index.html shown below:

<script>
$(document).ready(function() { 

$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box

$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});

</script>

<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>    
</div> 

<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select> 
</div>      

</body>
</html>    

sector_options.php shown below:

<?php

$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());

$query = "SELECT sector FROM Main_table ";

$result = mysqli_query($link, $query);

while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>
  ";
}

echo $options;

?>

subsector_options.php shown below:

<?php

$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());

$Sectors = $_REQUEST['filter_sector'];

$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";

$result = mysqli_query($link, $query);

while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>
  ";
}

echo $options;

?>
  • 写回答

1条回答 默认 最新

  • donoworuq450547191 2014-04-06 17:21
    关注

    For completeness, the solutions were:

    • Check how AJAX operations are doing using a browser network monitor
    • Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
    • AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?