我必须根据第一个下拉列表中的选择填充第二个下拉列表。我在下面给出的代码获得第一个下拉列表,但第二个下拉列表是空的。有人可以帮我解决这个问题吗?
HTML:
form name="myselection" action="myselection.php" method="get">
<select name="parent_cat" id="parent_cat" placeholder="product" required>
<option value="" selected="selected" disabled="true">Select One</option>
<?php while ($row=mysqli_fetch_array($statement)):?>
<option value="<?php echo $row['id'];?>"><?php echo $row['prodcat_name']; ?></option>
<?php endwhile ?>
</select>
<label>Sub Category</label>
<select name="sub_cat" id="sub_cat"></select>
<p><input type="submit"></p>
</form>
jQuery的:
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="img/loading.gif" alt="loading subcategory" /></div>');
$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});
});
</script>
loadsubcat.php
<?php
$parent_cat = $_GET['parent_cat'];
$conn = new mysqli("localhost", "root", "", "mydatabase");
$selection = "SELECT * FROM t_prodcategory WHERE prodcat_ID = {parent_cat}";
$statement= mysqli_query($conn,$selection);
while($row = mysqli_fetch_array($statement)) {
$pid=$row["prodcat_id"];
$product=$row["product"];
echo '<option value="'.$pid.'" >'.$product.'</option>';
}
?>