Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value in 2nd drop down box from first

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I'm having a slight problem. I have a form with 2 drop down boxes generated from a mysql database. This is no problem.
What I need it to do is this: when a value is chosen from the first drop down box I need the second one to show the selected value that corresponds to the id from the first.
here is the code to generate the boxes
Code:
<?php


<select name='class' id='class' >";
   $query='SELECT DISTINCT class_id, class_number 
           FROM tblClass_Details';
   $result=mysql_query($query);
?>						
<option selected value='0'>Select</option>
<?php while ($current_record=mysql_fetch_array($result)) {
	$id=$current_record['class_id'];
	$number=$current_record['class_number'];
?>					 			<option value='$id'>$number</option>
<?php									}
?>						
</select>							

<select name='teacher' id='teacher'>
<?php
   $query='SELECT DISTINCT teacher_id, first_name,last_name                          FROM tblTeacher';
   $result=mysql_query($query);
								?><option selected value='0'>Select</option>";
<?php	while ($current_record=mysql_fetch_array($result)) {
	   $id=$current_record['teacher_id'];
	   $first=$current_record['first_name'];
	   $last=$current_record['last_name'];
?>					 				

<option value='$id'>$first $last</option>
								<?php					}
								?>    </select>
I hope this is clear.
I believe that I need to use Javascript with an onchange event but have no idea how to implement it.
any help will be greatly appreciated.
 
you would either have to visit the javascript forum, or when the first box is changed, submit the form, then query the 2nd drop down according to the value of the 1st box value.

Regards,

Martin

Computing Help And Info:
 
What you are asking is not difficult. The following is a small test page that shows a working example based on your description above. It's Javascript...
Code:
<html>
<head>
<script type="text/javascript">
function setSelect(data) {
	var teacherObj = document.getElementById('teacher');
	for (var loop=0, len=teacherObj.length; loop<len; loop++) {
		if (teacherObj[loop].value == data) {
			teacherObj.selectedIndex = loop;
			break;
		}
	}
}
</script>
</head>
<body>
<form action="">
	<select id="class" onchange="setSelect(this.value)">
		<option value="12">id=12</option>
		<option value="15">id=15</option>
		<option value="19">id=19</option>
		<option value="22">id=22</option>
	</select>
	<select id="teacher">
		<option value="2">id=2 teacher</option>
		<option value="12">id=12 teacher</option>
		<option value="15">id=15 teacher</option>
		<option value="19">id=19 teacher</option>
		<option value="22">id=22 teacher</option>
		<option value="32">id=32 teacher</option>
	</select>
</form>
</body>
</html>
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top