Computer Learning Zone CLZ Access Excel Word Windows

Education's purpose is to replace an empty mind with an open one.

-Malcolm S. Forbes
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Active Server Pages Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Select Option Based on an Option Selected
Glenn Kaufman 
     
12 months ago
I have been working on a web page to enter city and state for a user. I have tables in my SQL database that have all 50 states and all the cities by state. I am stuck on how to filter the city select option based on the state selected.
Here is test page that will be moved to the main page after it works.
http://processsolution.net/selectstate.asp?User_ID=1
State Select
<option<% If rs2("StateID")=StateID Then Response.write " selected " %> value="<%=rs2("StateID")%>" onselect="SetState()"><%=rs2("State")%></option>
Select City Code
<tr><td>City: </td>
<td><select size="1" name="ID">
<%
Set Conn2 = Server.CreateObject("ADODB.Connection")
Conn2.Open CONNECTSTRING
Set rs2 = Server.CreateObject("ADODB.Recordset")
SQL2 = "SELECT * FROM tblCitiesByStates WHERE [State Name]=" & txtstate & "ORDER BY [City Name]"
rs2.Open SQL2, Conn2
While Not rs2.EOF
%>
<option
<% If rs2("ID")=StateID Then Response.write " selected " %> value="<%=rs2("ID")%>"><%=rs2("City Name")%>
</option>
<%
rs2.MoveNext
Wend
rs2.Close
Set rs2 = Nothing
Conn2.Close
Set Conn2 = Nothing
%>
</select></td></tr>
Joe Holland  @Reply  
      
12 months ago
There may be more issue but for sure you are missing a space before ORDER BY.
Donald Blackwell  @Reply  
       
12 months ago
Upon trying your test page, other than the missing space Joe pointed out, I looked at the page source.

Your options all call the onselect method of the option to call "SetState()" which would be how it would run code to populate the Cities, however, I can't see any inline JavaScript blocks or linked JavaScript that handles that method i.e. via an XMLHttpRequest to communicate with the server to (re)populate the cities box.

That's all that jumps out at me and I could be way off, and if so, sorry as I haven't worked with ASP since 1999.
Adam Schwanz  @Reply  
           
12 months ago
Yea you don't have any Javscript as Donald said. Can see in the network tab of the dev tools that no requests are being made when changing the state so it doesn't populate the city.
Adam Schwanz  @Reply  
           
12 months ago
Also without seeing everything, it looks like you are setting the value of the top combo  value="<%=rs2("StateID")%> to the stateID. But it appears by "State Name" you are calling for the string value in [State Name]=" & txtstate ? Unless State Name is StateID
Glenn Kaufman OP  @Reply  
     
12 months ago
I also noticed the missing space after I re-entered the code into the test page and fixed it after posting to the form, but thanks for the command. As for the SetState() I run out of room to post it
<%
Sub SetState()
Response.write rs2("State")
txtState = rs2("State") ' The selected state
Response.write txtState
End Sub
%>
Donald Blackwell  @Reply  
       
12 months ago
Your Sub is still a server side procedure (ASP). Your select box for the state can't see the "SetState()" ASP command so it doesn't do anything.

Once your server has generated the initial page with the state selector box, the web page has no way of communicating with the server directly unless you either reload the page, or have a JavaScript handler that would communicate with the server to update the content in the HTML Document Object Model.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Active Server Pages Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 2:59:53 PM. PLT: 1s