Dynamic ComboBox Linked DropDown Boxes using javascript, ASP and MSSQL

Posted on 2009-12-31
Last Modified: 2012-05-08
I need a way to link the dropdown boxes to each other. If the page is first loaded the user would have to select the country. Once the country is selected, the options for the state are populated  into the state dropdown box. Once the state is selected, the options for the city dropdown box is populated.

I need to do this without constantly refreshing the entire page or misfiring the rest of the form to be submitted.

I've seen this done with javascript using static arrays, but I need to have this done dynamically.

Below is the code for the form and to populate the dropdown boxes. Needed is the javascript and logic to link the dropdown boxes

Thanks in advance

set Conn = Server.CreateObject("ADODB.Connection")

Conn.Open ConString

SQL1 = "select distinct from zips order by country"

set rsCountry = Conn.Execute(SQL1)

SQL2 = "select distinct zips.state from zips where = '"&strcountry&"' order by state"

set rsState = Conn.Execute(SQL2)

SQL3 = "select distinct from zips where = '"&strcountry&"' AND zips.state = '"&strState&"' order by city

set rsCity = Conn.Execute(SQL3)


'dynamic COUNTRY dropdown box

<form name="form1" id="form1" action="anotherpage.asp" method="post">

<select name="strCountry">

<option>Select Country</option>



            while not rsCountry.EOF


<option value="<%= rsCountry("country")%>"><%=rsCountry("country")%>





            set rsCountry = Nothing



'dynamic STATE dropdown box

<select name="strstate">

<option>Select state</option>



            while not rsstate.EOF


<option value="<%= rsstate("state")%>"><%=rsstate("state")%>





            set rsstate = Nothing



'dynamic CITY dropdown box

<select name="strcity">

<option>Select city</option>



            while not rscity.EOF


<option value="<%= rscity("city")%>"><%=rscity("city")%>





            set rscity = Nothing



'other fields below

<input type="text" name="username">

<input type="text" name="email">

<input type="text" name="username">

<select name="Gender">

<option>Please Choose</option>

<option value="1">Woman</option>

<option value="2">Man</option>



<input type="submit" value="submit" name="B1"> 

<input type="reset" value="Reset" name="B2">




set conn = nothing


Open in new window

Question by:Patriotec
    LVL 22

    Accepted Solution


    You can achieve this using AJAX. Have  a look at this tutorial.
    LVL 3

    Expert Comment

    I agree with neeraj523.  But if at all if you do not want to use AJAX, you can follow following
    1. Get the counries, states and cities (or their ids if theare are any) into a hidden variable seperated by some delimiters.
    2. Read these using  javascipt into arrays that you creted.  After this you process is asual.
    LVL 3

    Expert Comment

    i recommend you to use JQuery for this, here is a sample for howto :

    this doesnt care on the Programming language, and fully support Ajax via POST,GET
    and even JSON transport.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now