You are here:

DHTML/Dynamic Dropdowns

Advertisement


Question
I am new to web design. What I am trying to do is create a dropdown based on a one field database table. Every time a choice is selected in that dropdown I need the second dropdown to be populated with values from the same table but limited by the value selected. For example lets say I have a table called Place I select the field called Location in the first dropdown, well in the second dropdow should be populated with location codes that match that Location selected. Pleas give an answer in vbscript but java script is acceptable.

Heres the real life code with just the first dropdown completed in an .asp page
<%@ Language=VBScript %>

<LINK rel="stylesheet" type="text/css" href="Style Sheet1.css">
<%'=Session("Conn")%>




<%'=cnly.connection%>

<%
Set cnLy=Server.CreateObject("adodb.Connection")
  'cnly.Open Session("appInfo")
  cnly.Open Session("Conn")
  'cnly.Open Session("Conn")
Set objRS = Server.CreateObject("ADODB.recordset")   
sql = "SELECT * FROM [interpstats].[DistrictNames08]"
'Response.Write sql
'responce.write cnly.ConnectionString

%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<META HTTP-EQUIV="REFRESH" CONTENT=300> <!--%'causes a page to refresh after 5 minutes %-->
<meta name="VI60_defaultClientScript" content="VBScript">

<%
If Not isDate(Request.QueryString("D")) then

  
Else
  Session("CurrentDate")=Request.QueryString("D")
  Request.QueryString("D")
End if %>

</HEAD>
<BODY>
<A href="Logoff.asp"><img src="Exit.gif" border=0></A><!--log off image -->
<P>   <%sTitle="<CENTER><FONT face=verdana><h3>Financial Services LLC<br>Financial Statistics</h3>" & FormatDateTime( Session("CurrentDate"),1)& "</FONT></CENTER>"
  Response.write sTitle & "<Center><FONT face=Tahoma size=2 color=red>" & Session("Fname") & " " &  Session("Lname") & " - " & Session("Language")& "</Font></Center>"
  ' prints firstname lastname and language of the user.
%>
<%Response.Write Request.QueryString("D")%>
<FONT face=Tahoma color=#00008b size=3>

<br>
<br>
<br>
<br>
<br>
<br>
<hr>
<form name = "sample" method ="get" action = "calendarrec.asp">
<%objRS.Open sql, cnly%>
<Select name="Id" onchange="this.form.submit();">
<option value = "" selected>Select a District</option>

<%While NOT objRS.EOF%>
<option value ="<%=objRS.Fields(0)%>"><%=objRS.Fields(0)%></option>
<% objRS.MoveNext
Wend
objRS.Close
Set objRS = Nothing
Set cnly=nothing
'Set objConn = Nothing%>
</SELECT>
</form>


Answer
Hi mo,

I don't have any VB experience, but found the following link.

http://www.javascriptkit.com/javatutors/selectcontent2.shtml

This example is in JavaScript and is very straightforward.  With a little time, you should be able to follow the logic and apply it to your VB page.

Andrew

DHTML

All Answers


Answers by Expert:


Ask Experts

Volunteer


Andrew Hoffman

Expertise

Ask me about JavaScript, CSS, HTML5/XHTML, or PHP. Chances are I'll be able to help you. I know the economy is in the tank, but if you like my answer, please consider donating a few bucks. It's like tipping your barista, if your barista served fresh code instead of coffee.

Experience

I started doing this when boy bands were still cool.

Education/Credentials
I read a lot of nerdy books to get here.

©2012 About.com, a part of The New York Times Company. All rights reserved.