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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

side by side dynamic drop down boxes 1

Status
Not open for further replies.
Nov 29, 2001
72
US
I need to be able to dynamically populate a drop down box based on a selection from another drop down box on the same form. I cannot separate these onto two forms for asthetic reasons.

The first drop down box has three static choices (Project, Admin, Help Desk) and based on what the user selects, I populate the second form from a SQL table. Again, these boxes are side by side on a single form.

I searched the archives but could only find the "two form" solution that I am already familiar with but can't use. Does anyone have a working example of how to do this on a single form or a close example I could use?

Thanks in advance,
Dave
 
I would write a stored proc to run and separate the tables into the 3 categories. Load all three tables on the load of the page. At this point JavaScript will be your best answer. If you use the onChange() function on the first drop down you can pass the value to a Case stmt. At this point you will populate the second drop down.
 
aaahh,
Sounds like a plan. I will implement and see if I can get it to work.

Thanks again,
Dave

entaroadun,
I am not familiar with drop-down with option groups. Could you elaborate?

Thanks,
Dave
 
Never mind. I didn't realize how ill-supported OPTGROUPs are. IE doesn't recognize them yet, nor does Netscape. Oh well.
 
Here is an example I have passed out several times on dynamic dropdowns. The basis is building javascript arrays, then when a selection is made from the first drop down, the second drop down is filled from the inner array for the first selection.
This example is for a page that has a dropdown for selecting a day and then a shift. The days could each have diferant shifts available. I did not include the sql or database connections in order to cut this down to a somewhat more digestible length:
Code:
<%
Option Explicit

Dim objRS

'objRS holds a record set that contains records with shift_day, shift_number
' for example
' 1st record: Monday, 1
' 2nd record: Monday, 2
' 3rd record: Monday, 3
' 4th record: Tuesday, 1
' etc
%>
<html>
<head>
<title> Shift Selection </title>
<script language=&quot;JavaScript&quot;>
<!--
var shifts;
	function init(){
		//to initialize them all to the same times
		shifts={dayOfWeek: [
		<%
		dim tDay
		objRS.MoveFirst
		Do Until objRS.EOF
			If tDay <> objRS(&quot;shift_day&quot;) Then	'If not equal to previous, than start a new array element
				%>
				{day: &quot;<%=objRS(&quot;shift_day&quot;)%>&quot;,theShifts: [
				<%
				tDay = objRS(&quot;shift_day&quot;)			
			End If
			%>&quot;<%=objRS(&quot;shit_number&quot;)%>&quot;, <%	'add the shift number to inner array
			objRS.MoveNext
			If tDay <> objRS(&quot;shift_day&quot;) Then	'If next not equal to current, end the array element
				Response.Write &quot;]},&quot;
			End If
		Loop
		%>
	}

	//change day function
	function changeDay(elem){
		var j, day;
		var shiftText;

		if(elem.selectedIndex == 0)	//if they selected our pretty [select a day] stmt
			return false;			//do nothing and leave quietly
		
		//Clear the second drop down of all but top [select a shift]
		for (i = frmFormName.workday.options.length - 1; i >= 1; i--) frmFormName.workday.options[i] = null;
		frmFormName.workday.selectedIndex = 0;

		//grab day from select box
		day = elem.selectedIndex-1;

		for(j=0;j<shifts.dayOfWeek[day].theShifts.length-1;j++){
			document.frmFormName.workday.options[j] = new Option(shifts.dayOfWeek[day].theShifts[j],&quot;&quot;);
		}
	}
//-->
</script>
</head>
<body onLoad=&quot;init();&quot;>
<form method=POST action=&quot;wherever.html&quot; name=&quot;frmFormName&quot;>

<select name=&quot;weekday&quot; onChange=&quot;changeDay(this);&quot;>
	<option>[Select a day]</option>
	<%
	objRS.MoveFirst
	tDay = &quot;&quot;
	Do Until objRS.EOF
		If tDay <> objRS(&quot;shift_day&quot;) Then
			%>
			<option><%=objRS(&quot;shift_day&quot;)%></option>
			<%
			tDay = objRS(&quot;shift_day&quot;)
		End If
		objRS.MoveNext
	Loop
	%>
</select>
<select name=&quot;workday&quot;>
	<option>[Select a Shift]</option>
</select>
</form>
</body>
</html>

Hope that helps,
feel free to ask questions if you decide to use it and get stuck,
-Tarwn &quot;If you eat a live toad first thing in the morning, nothing worse will happen all day long.&quot; - California saying
&quot;To you or the toad&quot; - Niven's restatement of California saying
&quot;-well most of the time anyway...&quot; - programmers caveat to Niven's restatement of California saying
(The Wiz Biz - Ri
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top