Dynamically Dependent Dropdown Lists

You can test the demo for dynamically dependent dropdown boxes here:
http://www.jamesfarrow.com/codeexamples/Double_Listbox_Populated_From_DB/

Download Source & Access Database Used:
http://www.jamesfarrow.com/codeexamples/Double_Listbox_Populated_From_DB/this.zip.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Dependent Dropdownlists</title>


<%
Dim myConnection 'Object
Dim myRS 'Object
Dim myConnectString 'String
Dim mySQL 'String
Dim StartAuthor 'String
Dim Cnt 'Integer
Dim ListBox1 'String

'Database Connect String
myConnectString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("author.mdb")
'Make Javascript Function to handle ListBox 2
%>
<script language="javascript">
function listboxchange(p_index)
{
<%
'SQL Statement which groupes all authors together, to handle more than 1 book per author
mySQL = "SELECT * FROM Library Order By Author asc"

Set myConnection = CreateObject("ADODB.Connection")
myConnection.open myConnectString
'Get Recordset using our SQL
Set myRS = myConnection.Execute(mySQL)
%>
switch (p_index)
{
<%
'Loop Through all authors and books
Do Until myRS.eof
StartAuthor = myRS("Author")
Cnt = 0

%>
case "<%=StartAuthor%>" :
<%
'Loop until we disover a new author. Whilst author the same, add all of his/her books into grouped selection
do until myrs.eof or StartAuthor <> myRS("Author")
%>
document.myform.listbox2.options[<%=cnt%>]=new Option("<%=myRs("Book")%>","<%=myRs("Book")%>");
<%
Cnt = Cnt + 1
myrs.movenext
if myrs.eof then exit do
Loop
%>
break;
<%
Loop
myRS.close
Set myRS = nothing
%>
}
return true;
}
</script>


<%
'Make ListBox1
'SQL Statement which selects each database entry for an author just once
'as we don't want the same name twice in our list box

mySQL = "SELECT DISTINCT Author FROM Library Order By Author asc"

Set myConnection = CreateObject("ADODB.Connection")
myConnection.open myConnectString
Set myRS = myConnection.Execute(mySQL)

'Make Drop down box Author list
do until myrs.eof
ListBox1 = ListBox1 & "<option value=""" & myRs("Author") & """>" & myRs("Author") & "</option>"
myRs.movenext
loop

myRS.close
Set myRS = nothing
myConnection.close
Set myConnection = nothing
%>
</head>
<body>
<form name="myform" action="" method="get">
<select name="listbox1" id="listbox1" onchange="javascript: listboxchange(this.options[this.selectedIndex].value);">
<option value="">Please Select</option>
<%=ListBox1%>
</select>

<select name="listbox2">
<option value="">Please Select</option>
</select>
</form>


</body>
</html>

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%