3 Replies - 50682 Views - Last Post: 25 January 2009 - 03:25 AM Rate Topic: -----

#1 bstonehill   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Populate Dropdown based on another Dropdown

Posted 21 January 2008 - 01:56 PM

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!

<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>


Is This A Good Question/Topic? 1

Replies To: Populate Dropdown based on another Dropdown

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6876
  • View blog
  • Posts: 31,668
  • Joined: 10-May 07

Re: Populate Dropdown based on another Dropdown

Posted 21 January 2008 - 02:00 PM

View Postbstonehill, on 21 Jan, 2008 - 01:56 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#3 didgy58   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: Populate Dropdown based on another Dropdown

Posted 23 January 2008 - 09:12 AM

hi there bstonehill ive done something like this with a javascript function that submits the form when the select box value changes
<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

</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
$[email protected]$_GET['cat']; 
$[email protected]$_GET['subcat'];
$[email protected]$_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
Was This Post Helpful? 0
  • +
  • -

#4 rv.kalpana   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 25-January 09

Re: Populate Dropdown based on another Dropdown

Posted 25 January 2009 - 03:25 AM

View Postbstonehill, on 21 Jan, 2008 - 12:56 PM, said:

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!

<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>



Hi evryone.. even my need is the same .. but i need the code in jsp instead of php.. can anyone help me?
Was This Post Helpful? 1

Page 1 of 1