JS
<script type="text/javascript">
$(document).ready(function(){
$("#FirstDD").change(function(){
$('#SecDD').load('inc/subcategories.php?scatID='+this.value);
});
});
</script>
PHP
<select style="width:300px;" id="FirstDD" name="userListingCategory">
<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
#GET SELECT category names
$pdo2 = new PDO($h1, $u, $p);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare("
SELECT catID, catName FROM Category
;");
$sth2->execute(array());
#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
AND SC.catID = '$categoryID'
;");
$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);
?>
问题我选择了first dropdown category
,然后2nd dropdown
清除了,但没有填充subcategories.php
的值。我认为这是因为$sth2
正在unset()
并且之后尝试使用$categoryID
变量(在$sth3 query
中)。我该如何解决这个问题?
答案 0 :(得分:0)
我认为以下问题是让您的代码无法按照您的想法运行:
在“PHP”中:
在尝试使用它之前,您需要设置$ sth3。
我建议移动线
<?php require_once('inc/subcategories.php'); ?>
到线以上的某个地方:
<?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
接下来我认为您需要更改此行:
{echo "<option value=". $categoryID . ">" .$row['catName']."</option>";}
到
{echo "<option value=". $row['catID'] . ">" .$row['catName']."</option>";}
在subcategories.php中:
我没有看到$ categoryID的设置位置。因此,如果没有从GET参数设置,则添加:
if(!empty($_GET['scatID'])) $categoryID = (int)$_GET['scatID'];
else $categoryID = -1; // Something bogus
前
#GET SELECT category names
$pdo2 = new PDO($h1, $u, $p);