Jump to content

help with dynamic dropdowns in php


stmosaic

Recommended Posts

I've been reworking a script that when you select a category in one dropdown box, you are the presented with the category you selected and a second dropdown of those subcategories that are applicable. The original worked but not in a useful manner. It pulls from a simple table of id, category and subcategory. Can anyone see where I am going astray? I appreciate you taking the time to help me with this!

<?php
mysql_connect("localhost","UN","PW");
mysql_select_db("dbName");
$getcat = mysql_query("select DISTINCT category from categories ORDER BY category") or die(mysql_error());

$getsubcat = mysql_query("select * from categories WHERE category = '".$_POST["category"]."' ORDER BY subcategory") or die(mysql_error());

if (isset($_POST["category")){
$selectedcat = $_POST["category"];
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="check" method="post">
    <!--- First DropDown starts here when no category has been selected--->
<?php
if (!isset($_POST["category"])){
echo "<select name=\"category\" onChange=\"this.form.submit()\"><option value="none">Select a Category</option>";
  
    while($res = mysql_fetch_array($getcat)){
  echo "<option value=\"$res['category']\"><$res['category']</option>";
    }

echo "</select>";		
}
?>
    <!-- First DropDown ends here -->

    <!-- Second DropDown starts here with previously selected category displayed-->
        <?php if (isset($selectedcat)){
	echo "<select name=\"category\">
  <option value=\"$selectedcat\">$selectedcat</option>";
    }
?>

        <select name="subcategory" onChange="add_form.php">
<option value="0">Select a Subcategory</option>
            <?php while($row = mysql_fetch_array($getsubcat)){?>
                <option  value="<?php echo $row["id"]?>"><?php echo $row["subcategory"]?></option>
            <?php } unset($getsubcat,$row);?>
        </select>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/46133-help-with-dynamic-dropdowns-in-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.