[英]Populate 2nd Dropdown based on 1st Dropdown Choice
Basically, what I'm trying to accomplish is when someone clicks the first html dropdown id="FirstDD"
, based on the option
chosen the second dropdown id="SecDD"
would populate with the fields that correlate to FirstDD
dropdowns choice. 基本上,我要完成的工作是,当有人单击第一个html下拉列表id="FirstDD"
,根据选择的option
,第二个下拉列表id="SecDD"
将填充与FirstDD
下拉列表选项相关的字段。 Triggering this all is ajax to do the initial call and php to handle from there in subcategories.php
. 触发所有这些操作就是ajax进行初始调用,然后从subcategories.php
php处理。
Currently, what is happening is: i selected the first dropdown, but nothing gets populated in SecDD
目前,发生的事情是:我选择了第一个下拉列表,但是SecDD
没有填充任何SecDD
JS/AJAX: JS / AJAX:
<script type="text/javascript">
$(document).ready(function(){
$("#FirstDD").change(function(){
$('#SecDD').load('inc/subcategories.php?scatID='+this.value);
});
});
</script>
HTML 的HTML
<select style="width:300px;" id="FirstDD" name="userListingCategory">
<!--onchange="$('#SecDD').load('inc/subcategories.php?scatID='+this.value);"-->
<option disabled="disabled">Category...</option>
<?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
{echo "<option value=". $categoryID . ">" .$row['catName']."</option>";}
unset($sth2);
?>
</select>
<select style="width:340px;" id="SecDD" name="userListingSCategory" style="display:none">
<?php require_once('inc/subcategories.php'); ?>
</div>
</select>
Subcategories.php Subcategories.php
<?php require_once('db/dbc.php');
#GET SELECT sub-category names
$pdo3 = new PDO($h1, $u, $p);
$pdo3->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth3 = $pdo3->prepare("
SELECT scatID, scatName
FROM Category C, SubCategory SC
WHERE C.catID = SC.catID
;");
$sth3->execute(array());
?>
<option disabled="disabled">Sub-Category...</option>
<?php
#Get subcats
while($row = $sth3->fetch(PDO::FETCH_ASSOC))
{echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";}
unset($sth3);
?>
Diagnose the issue: 诊断问题:
If both of those work fine each time, then something's not triggering, but the code seems sound from here. 如果这两种方法每次都能正常工作,则说明没有什么触发条件,但是代码似乎从这里发出。 After that, I would try putting echo statements in between your lines and see where it stops. 之后,我将尝试将echo语句放在行之间,并查看其停止位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.