I want to get the category_id of the selected category and pass it to next query of subcategory so the next combo box will contain subcategories according to the selected category. Can you please help me to do it. Here is my javascript, php and html code.
<script language="javascript" type="text/javascript">
function showCompany(catid) {
document.form.submit();
}
</script>
<select name="cat_id" id="cat_id" onChange="showCompany(this.value);">
<option value="">--Select--</option>
<?php
$sql_row1 = mysqli_query($mysqli,"SELECT * from categories");
while($sql_res1=mysqli_fetch_array($sql_row1))
{
?>
<option value="<?php echo $sql_res1["category_id"]; ?>"><?php echo $sql_res1["category"]; ?></option>
<?php
}
?>
</select>
<select name="company_id" id="company_id">
<option value="">--Select--</option>
<?php
$sql_row = mysqli_query($mysqli,"SELECT * from subcategories where category_id='$_REQUEST[cat_id]'");
while($sql_res=mysqli_fetch_array($sql_row))
{
?>
<option value="<?php echo $sql_res["subcategory_id"]; ?>"><?php echo $sql_res["subcategory_name"]; ?></option>
<?php
}
?>
</select>
Edit this line to this:
$sql_row = mysqli_query($mysqli,"SELECT * from subcategories where category_id='".$_REQUEST['cat_id']."'");
Providing $_REQUEST['cat_id'] has a value, it should now go in the query properly.
Try removing single quotes around $_REQUEST. may be the column type on DB is int. Try following code.
<?php
$cat_id=$_REQUEST['cat_id'];
$sql_row = mysqli_query($mysqli,"SELECT * from subcategories where category_id=$cat_id");