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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度