I have to make a form with multiple drop-down menus that modify one another. The menus/database tables are:
Categories, Styles, Types, and Mechanisms
I have attempted to do this with my limited knowledge of Ajax, but can only seem to access MySQL once (on the initial page) to populate the Categories table without being able to then update the Styles table by querying for the next set of results. I receive an error that claims the database is empty.
I also tried having the drop-down populated through an option group to handle both the Categories and Styles with a looped query, but only the Category headings show up with all of the Style sub-values showing up blank. My code is as follows:
$query1="SELECT categories.category_id, categories.Category_Name ";
$query1.="FROM categories ";
$query1.="ORDER BY categories.Category_Name ASC";
$category_result=mysql_query($query1, $connection);
if(!$category_result){
die("Database query failed: " . mysql_error());
}
$options="";
$con=0;
while ($category_row=mysql_fetch_array($category_result)) {
$category_name=$category_row["Category_Name"];
$CategoryID=$category_row["category_id"];
$options.="<OPTGROUP LABEL=\"$category_name\"> <br />";
$query2="SELECT categories.category_id, categories.Category_Name, ";
$query2.="styles.style_id, styles.Style_Name ";
$query2.="FROM categories, styles ";
$query2.="WHERE styles.Category_ID = $CategoryID ";
$style_result=mysql_query($query2, $connection);
if(!$style_result){
die("Database query failed: " . mysql_error());
}
while ($style_row=mysql_fetch_array($style_result)) {
$style_name=$row["Style_Name"];
$id=$row["style_id"];
$options.="<OPTION VALUE=\"$id\" <a href=\"#\" onClick=\"javascript:swapContent('$style_name');\" >".$style_name.'</OPTION>';
}
$options.='</OPTGROUP> <br />';
}
?>
<SELECT NAME="category_id">
<OPTION VALUE=0></OPTION>
<?php echo $options ?>choose
</SELECT>
Any insight into what I am doing wrong would be greatly appreciated!