<html>
<head>
<title>A Three Level Drop-Down System </title>
</head>
<body>
<p>What Kind of food do you like?</p>
<form name=form1 method=post>
<%
set conn = server.createobject("ADODB.Connection")
Conn.open("driver={SQL Server};server=xxxx;uid=xxxx;pwd=xxxx;database=xxxx")
if request.form("Level3") > "" then
strSQL = "Select * from tbCategories where UniqueID=" & request.form("Level3")
set rs = conn.execute(strSQL)
if not rs.eof then
Response.write("<h2>You Like " & rs("Description") & "</h2>")
Response.write("<input type=button value='Go Back' onclick=window.location='multi_level_dropdown.asp'>")
end if
else
strSQL = "Select * from tbCategories where CategoryLevel = 1"
set rs = conn.execute(strSQL)
%>
<!--
The section creates the Drop-Down for the first level. This code simply creates
the Select tag with an
onChange Event that will reload the page with a querystring equal to the value
of the item selected.
If there is an item selected (The Querystring matches and item in the list) then
that item is selected.
You may notice that I am not putting the values in quotes. This makes the values
numeric which
means that I also must compare to a numeric version of the Querystring so I use
the CINT() function to
convert the querytstring to an Integer.
-->
<Select name='Level1' onChange=window.location='multi_level_dropdown.asp?Level1='+this.value>
<option value=0>Select an Category</option>
<%
do until rs.eof
if rs("CategoryID") = cInt(request.querystring("Level1")) then
response.write("<option Selected value=" &rs("CategoryID")& ">" &
rs("Description") & "</option>")
else
response.write("<option value=" &rs("CategoryID")& ">" & rs("Description") &
"</option>")
end if
rs.MoveNext
loop
%>
</Select>
<%
rs.close
set rs = nothing
%>
<!--
This section is for the second drop-down box. Notice that it is pretty much a
replica of the first drop-down box section.
The main difference being that the field names have been changed and we had to
add a bit to the onChange statement in the
Select tag to include both the level1 and level2 values
-->
<%
if cint(request.querystring("Level1")) > 0 then ' Check to see if Level 1 has
been processed (querystring for Level1 exists)
strSQL = "Select * from tbCategories where CategoryID=" & request.querystring("Level1")
& " and CategoryLevel = 2"
set rs = conn.execute(strSQL)
%>
<select name='Level2'
onchange=window.location='multi_level_dropdown.asp?Level1=<%=request.querystring("Level1")%>&Level2='+this.value>
<option value=0>Select a sub-Category</option>
<%
do until rs.eof
if rs("SubCategoryID") = cint(request.querystring("Level2")) then
response.write("<option Selected value=" &rs("SubCategoryID")& ">" &
rs("Description") & "</option>")
else
response.write("<option value=" &rs("SubCategoryID")& ">" & rs("Description") &
"</option>")
end if
rs.MoveNext
loop
%>
</select>
<%
rs.close
set rs=nothing
end if
%>
<!--
Now we do the same thing for Level 3. We could do so for Levels 4,5,6 ... The
only thing that changes is the
onChange function of the <Select> tag has to include values for all previous
levels. Since this is the last level
I don't actually need an OnChange function. You need it on up to the last level,
but on the last level the user
will usually click a submit button which will process the selected data. In this
case I used the onChange
function of the Last level to make the submit button visible.
Note I broke up the SQL query line to makeit easier to read.
-->
<%
if cint(request.querystring("Level2")) > 0 then ' Check to see if Level 2 has
been processed (querystring for Level2 exists)
strSQL = "Select * from tbCategories "
strSQL = strSQL & "where CategoryID=" & request.querystring("Level1")
strSQL = strSQL & " and SubCategoryID=" & request.querystring("Level2")
strSQL = strSQL & " and CategoryLevel = 3"
set rs = conn.execute(strSQL)
%>
<select name='Level3' onChange=document.form1.btnSubmit.style.display=''>
<option value=0>Make your Final Choice</option>
<%
do until rs.eof
response.write("<option value=" &rs("UniqueID")& ">" & rs("Description") &
"</option>")
rs.MoveNext
loop
%>
</select>
<%
rs.close
set rs=nothing
%>
<br>
<input type=submit value="Submit"
name="btnSubmit" style="display:none;">
<%
end if
%>
</form>
<%
end if
%>
</body>
</html>