I have a page to add products. On this page is a 'categorey' dropdown and a 'subcategory' dropdown.
The subcategory dropdown should fill with the options based on the category dropdown selection.
The code I have so far does not fill the subcat dropdown, can anyone see a problem?
code
include 'connect.php':
if(isset($_POST['cat'])){
$cat = intval($_POST['cat']);
$query = "SELECT subcat_id, subcategory FROM `subcategories` WHERE cat_id = $cat";
$result = $link->query($query) or die('error');
$subcatOps = '';
if(mysqli_num_rows($result)){
while ($row = mysqli_fetch_assoc($result)) {
$subcatOps .= "\n\t<option value='{$row['subcat_id']}'>{$row['subcategory']}</option>";
}
}
echo $subcatOps;
}
annd the form
<form class='adminform1' action='add_products.php' method='post' enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<?php
include '../inc/categorydropdown.php';
?>
<span class='formheading'>Add Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file" id="image_upload_box" /></p>
<b>Name</b><br /><input type=text name="aname" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<b>Product Code</b><br /><input type=text name="acode" /><br />
<p><label for="cat">Category</label>
<select name="cat" id="cat">
<?php echo $op;?>
</select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="subcat"> </select></p>
<br />
<br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
</form>
<script>
//bind category dropdown change event
$('#cat').change(function(){
getSubCat();
});
//change subcat dropdown
function getSubCat(){
cat = $('#cat').val();
$.post("../inc/subcat.php", { cat: cat }).done(function(data) {
$('#subcat').html(data);
});
}
//run on page load
getSubCat();
</script>
Thanks for looking.