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);
?>
Problem I'm having I select the first dropdown category
, then the 2nd dropdown
clears out but doesnt populate with the values of subcategories.php
. I think this is because $sth2
is being unset()
and trying to use $categoryID
variable after that (in the $sth3 query
). How do I fix this??
Here are the issues that I imagine are keeping your code from working how you think it should:
In "PHP":
You need the $sth3 be set before attempting to use it.
I suggest moving the line
<?php require_once('inc/subcategories.php'); ?>
to somewhere above the line:
<?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
Next I think you need to change this line:
{echo "<option value=". $categoryID . ">" .$row['catName']."</option>";}
to
{echo "<option value=". $row['catID'] . ">" .$row['catName']."</option>";}
In subcategories.php:
I don't see where $categoryID is being set. So if it is not being set from the GET params then add:
if(!empty($_GET['scatID'])) $categoryID = (int)$_GET['scatID'];
else $categoryID = -1; // Something bogus
before
#GET SELECT category names
$pdo2 = new PDO($h1, $u, $p);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.