?
Solved

Dynamic ComboBox Linked DropDown Boxes using javascript, ASP and MSSQL

Posted on 2009-12-31
3
Medium Priority
?
1,244 Views
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 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
Comment
Question by:Patriotec
3 Comments
 
LVL 22

Accepted Solution

by:
neeraj523 earned 2000 total points
ID: 26157407
Hello

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

http://skeymedia.com/classic-asp-and-ajax-tutorial/
0
 
LVL 3

Expert Comment

by:crshekharam
ID: 26162472
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
 
LVL 3

Expert Comment

by:roeib
ID: 26162513
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 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