Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,427 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,894 people online right now. Registration is fast and FREE... Join Now!




Populate Dropdown based on another Dropdown

 
Reply to this topicStart new topic

Populate Dropdown based on another Dropdown

bstonehill
21 Jan, 2008 - 12:56 PM
Post #1

D.I.C Head
Group Icon

Joined: 21 Jan, 2008
Posts: 53



Thanked: 1 times
Dream Kudos: 25
My Contributions
I have a form with 2 dropdown boxes, [category] and [item]. [category] looks up available values in a MySQL database. When [category] changes I need to repopulate [item] with items from the database that match the selected category, but I'm not sure how to pass the value from [category] to PHP to perform the query.

Any help would be greatly appreciated!

CODE
<div align="center">
<form action='submit_order.php' method='post' name='addItems'>
    <table width="600" border="0" cellspacing="3" cellpadding="3" align="center">
          <?php echo "
        <tr>
        <td><label for='po_number'>PO Number </label><input type='text' value='$next' size='15' /></td>
        <td><label for='vendor'>Vendor </label>
            <select name='vendor'>
                <option></option>";
                $sql = "SELECT name FROM vendors ORDER BY name"; //Populate Vendor Dropdown
                $result = mysql_query($sql);                            
                while($row = mysql_fetch_row($result)){
                    echo "<option>$row[0]</option>";
                    } echo "
            </select></td>
           <td><label for='user'>Placed By </label><input type='text' value='$placedby' size='15' /></td>
        </tr>
          <tr><td> </td></tr>
        <tr>
           <td><label for='category'>Category     </label>
            <select name='category' onchange='changeCat()'>
                <option></option>";
                $sql = "SELECT category FROM categories ORDER BY name"; //Populate Category Dropdown
                $result = mysql_query($sql);                            
                while($row = mysql_fetch_row($result)){
                    echo "<option>$row[0]</option>";
                    } echo "
            </select></td>
           <td><label for='item'>Item    </label>
            <select name='item'>
                <option></option>";
                $sql = "SELECT name FROM pricebook WHERE category = '$category'"; //Populate Item Dropdown
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                    echo "<option>$row[0]</option>";
                    } echo "</td>
        <td><label for='cost'>Cost  </label><input type='text' name='cost' size='15'></td>
           <td><label for='qty'>Quantity </label><input type='text' name='qty' value=0 size='8'></td>
        <td><label for='ext'>Ext Cost</label><input type='text' name='ext' value=0 size='10'</td>
        <td><img src='images/add.gif' width=25 height=25 onclick='java script:addRow();'></td>
        </tr>";
        ?>
    </table>
</form>
</div>

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Populate Dropdown Based On Another Dropdown
21 Jan, 2008 - 01:00 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,550



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(bstonehill @ 21 Jan, 2008 - 01:56 PM) *

When [category] changes I need to repopulate [item] with items from the database that match the selected category, but I'm not sure how to pass the value from [category] to PHP to perform the query.


Use onChange() or something written in Ajax.

User is offlineProfile CardPM
+Quote Post

didgy58
RE: Populate Dropdown Based On Another Dropdown
23 Jan, 2008 - 08:12 AM
Post #3

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 171



Thanked: 1 times
My Contributions
hi there bstonehill ive done something like this with a javascript function that submits the form when the select box value changes
CODE


<script language="javascript">
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
var val3=form.subcat1.options[form.subcat1.options.selectedIndex].value;

self.location='dd.php?cat=' + val + '&subcat=' + val2 + '&subcat1=' + val3;

}

</script>



as you can see thats the javascript function above that when the form reloads, its takes the value from each of the selects and stores then in val. i have done this to work with 3 dynamic selects but the only trouble i have with this is the fact that i cant get the value of all three to save in the end and create a SQL statment out of them all as it only seems to save the last select box that has been changed.

here is the rest of the code for the page

CODE

</head>
<body>
<div id="wrapper">
<div id="header">
<h1>Tomorrow's Toys Today</h1>
</div>
<div id="subheader">
<?php include 'submenu.php'?>
</div>
<div id="leftmenu">

<?php
$cat=@$_GET['cat'];
$subcat=@$_GET['subcat'];
$subcat1=@$_GET['subcat1'];



///////// Getting the data from Mysql table for first list box//////////
@$quer=mysql_query("SELECT *  FROM Manufacturer");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
@$quer2=mysql_query("SELECT Product_name  FROM Product where Manu_id = '$cat'");
}else{$quer2=mysql_query("SELECT Product_name  FROM Product where Manu_id= '$cat'"); }
////////// end of query for second subcategory drop down list box ///////////////////////////

///// third drop down box query again check if the category has been selected else we will display all the subcategory////

if(isset($subcat) and strlen($subcat) > 0){
@$quer3=mysql_query("SELECT Capacity  FROM Product where Product_name = '$subcat'");
}else{$quer3=mysql_query("SELECT Capacity  FROM Product where Product_name= '$subcat'"); }

//end of form////

echo "<form method=post name=f1 action='dd-check.php'>";
echo "<table> <tr><td>";






//////////        Starting of first drop downlist /////////
echo "Manufacturer";
echo"</td><td>";
echo "<select name='cat' onchange=\"reload(this.form)  \"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
if($noticia['Manufacturer=']==$cat){
        echo "<option value='$noticia[Manu_id]'>$noticia[Manufacturer]</option>"."<BR>";}
    else{
        echo  "<option value='$noticia[Manu_id]'>$noticia[Manufacturer]</option><br>";}
}
echo "</select></td></tr><tr><td>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "Product Name";
echo "</td><td>";
echo "<select name='subcat' onchange=\"reload(this.form)  \"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['Product_name=']==$subcat){
        echo "<option value='$noticia2[Product_name]'>$noticia2[Product_name]</option>"."<BR>";}
    else{
        echo  "<option value='$noticia2[Product_name]'>$noticia2[Product_name]</option>";}
}
echo "</select></td></tr><tr><td>";
//////////////////  This will end the second drop down list ///////////

//// starting third drop down list//////
echo " Capacity ";
echo "</td><td>";
echo "<select name='subcat1' onchange=\"reload(this.form)  \"><option value=''>Select one</option>";
while($noticia3 = mysql_fetch_array($quer3)) {
echo  "<option value='$noticia3[Capacity]'>$noticia3[Capacity]</option><br>";
}
echo "</select></td></tr><tr><td>";
echo "<input type=submit value=Show></td></tr></table>";
echo "<input type=text name=cats value=$cat>";
echo "</form>";

?>

</div>
<div id="main">

<h1>how to use this method</h1>
as you can see this method uses dynamic drop down boxes so when you make the first selection from the first drop down box the others will fill in with the previous selection.
</div>

</div>


if anybody might know how to adapt this at all ive tried looking into ajax but im kinda stuck on it so if there is anybody out there who could break it down for me as how to do it in ajax i would be much appreciated.

but i hope this helps you out with what you need.

Dan

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month