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 KaufmanOP
@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.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Active Server Pages Forum.