Link to home
Create AccountLog in
Avatar of BIGELOJ
BIGELOJFlag for Canada

asked on

Populate Second Select List based on value in first select list.

I want to have a 2nd Value List populate based upon the value selected in the first Value List.
I am just going to show you the code that relates to my dilemma. Some of the code that is shown I pulled from another area of Experts Exchange.

//Get Data for Category Select List
mysql_select_db($database_tracc, $tracc);
$query_race_classes = "SELECT class_code, class_description FROM race_classes";
$race_classes = mysql_query($query_race_classes, $tracc) or die(mysql_error());
$row_race_classes = mysql_fetch_assoc($race_classes);
$totalRows_race_classes = mysql_num_rows($race_classes);

//Query Database to populate second Select List based on value in First Select List
// was a choice already made for the second box?

if (isset($_POST['racing_class']) && $_POST['racing_class']) {
mysql_select_db($database_tracc, $tracc);
// get the choices for the second box, based on the choice in the first box
// REMEMBER TO VALIDATE YOUR $_POST DATA!  I just used simple typecasting here...
$query_class_cost = "SELECT * FROM class_rates WHERE class_code=" . (int)$_POST['racing_class'];
$class_cost = mysql_query($query_class_cost, $tracc) or die(mysql_error());
$row_class_cost = mysql_fetch_assoc($class_cost);
$totalRows_class_cost = mysql_num_rows($class_cost);
}
echo $row_class_cost;
?>

Open in new window


HTML CODE:
<!--Begin Class Group-->
	<div class="control-group">
	<label class="control-label ps5" for="racing_class">Racing Class</label>
	<div class="controls">
	<select name="racing_class" required="required" id="racing_class">
			<?php
		do {  
		?>
			<option value="<?php echo $row_race_classes['class_code']?>"><?php echo $row_race_classes['class_description']?></option>
			<?php
				} while ($row_race_classes = mysql_fetch_assoc($race_classes));
				  $rows = mysql_num_rows($race_classes);
				  if($rows > 0) {
					  mysql_data_seek($race_classes, 0);
					  $row_race_classes = mysql_fetch_assoc($race_classes);
				  }
				?>
	</select>
	</div>
	</div>
	<!--End Class Group-->
	
	<!--Begin Duration Group - Weekend or Day-->
	<div class="control-group">
	<label class="control-label ps5" for="racing_duration">Racing Duration</label>
	<div class="controls">
	<select name="racing_duration" required="required" id="racing_duration">
			<?php
do {  
?>
			<option value="<?php echo $row_class_cost['rate_description']?>"><?php echo $row_class_cost['rate_description']?></option>
			<?php
				} while ($row_class_cost = mysql_fetch_assoc($class_cost));
				  $rows = mysql_num_rows($class_cost);
				  if($rows > 0) {
					  mysql_data_seek($class_cost, 0);
					  $row_class_cost = mysql_fetch_assoc($class_cost);
				  }
				?>
	</select>
	</div>
	</div>
	<!--End Duration Group-->

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of BIGELOJ

ASKER

Your direction to http://www.9lessons.info/2010/08/dynamic-dependent-select-box-using.html was invaluable.

My project now works properly.

Thank You!
Thanks for the points -- sometimes it's all about how you ask the question!  Best, ~Ray