• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

Dynamic ComboBox Linked DropDown Boxes using javascript, ASP and MSSQL

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 zips.country from zips order by country"
set rsCountry = Conn.Execute(SQL1)

SQL2 = "select distinct zips.state from zips where zips.country = '"&strcountry&"' order by state"
set rsState = Conn.Execute(SQL2)

SQL3 = "select distinct zips.city from zips where zips.country = '"&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>
        <%
            rsCountry.MoveFirst
            while not rsCountry.EOF
        %>
<option value="<%= rsCountry("country")%>"><%=rsCountry("country")%>
        <%
            rsCountry.MoveNext
            wend
            rsCountry.close
            set rsCountry = Nothing
        %>
</select>

'dynamic STATE dropdown box
<select name="strstate">
<option>Select state</option>
        <%
            rsstate.MoveFirst
            while not rsstate.EOF
        %>
<option value="<%= rsstate("state")%>"><%=rsstate("state")%>
        <%
            rsstate.MoveNext
            wend
            rsstate.close
            set rsstate = Nothing
        %>
</select>

'dynamic CITY dropdown box
<select name="strcity">
<option>Select city</option>
        <%
            rscity.MoveFirst
            while not rscity.EOF
        %>
<option value="<%= rscity("city")%>"><%=rscity("city")%>
        <%
            rscity.MoveNext
            wend
            rscity.close
            set rscity = Nothing
        %>
</select>

'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>
</option>
</select>

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

</form>

<%
conn.close
set conn = nothing
%>

Open in new window

0
Patriotec
Asked:
Patriotec
1 Solution
 
neeraj523Commented:
Hello

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

http://skeymedia.com/classic-asp-and-ajax-tutorial/
0
 
crshekharamCommented:
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.
0
 
roeibCommented:
i recommend you to use JQuery for this, here is a sample for howto :

http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html

this doesnt care on the Programming language, and fully support Ajax via POST,GET
and even JSON transport.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now