Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating country/city select boxes

Status
Not open for further replies.

TribeMan

Programmer
Nov 16, 2009
22
0
0
IL
I have a mySQL table with a list of countries, and a second table with a list with cities.

To display the countries, I call the following code:
Code:
<%
Function list_CountryOptions(ByVal intSelectedCountry)
	Dim tempRS
	intSelectedCountry = Trim(intSelectedCountry)
	
	If Not util_IsValidID(intSelectedCountry) Then
		intSelectedCountry = util_GetCountry()			
	End If
	
	''refresh application value?
	If Application("Countries_" & util_GetLanguage()) = "" Or Request.QueryString("init") <> "" Then
		Call dalList_GetCountryList( _
				  list_GetLanguageColumnName(), _
				  list_GetLanguageColumnName(), _
				  tempRS)
							  
		list_CountryOptions = list_GetListOptions(tempRS)
				
		Set tempRS = Nothing	
		Application.Lock
			Application("Countries_" & util_GetLanguage()) = list_CountryOptions
		Application.UnLock
	Else
		''get value
		list_CountryOptions = Application("Countries_" & util_GetLanguage())	
	End If
	
	''mark selected ID option
	list_CountryOptions = Replace(list_CountryOptions, _
										"""" & intSelectedCountry & """", _
										"""" & intSelectedCountry & """ SELECTED")

End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%>
And to call the cities, I call the following function:
Code:
Function list_CityOptions(ByVal SelectedID)
	list_CityOptions = list_CityOptionsForCountry(SelectedID, _
									util_GetCountry(), util_GetLanguage())
End Function
I now wish to populate 2 select boxes (javascript) so that when a specific country displays in the 1st box, the cities for taht country will display in the second.

Any help will be appreciated.
 
Oops! Left out this part of the City code:
Code:
Function list_CityOptionsForCountry(ByVal SelectedID, ByVal intCountryID, ByVal intLanguageID)
	Dim tempRS
	Dim strOptions, strListItems
	
	strOptions = ""
	''refresh application value?
	Call list_CityListsForCountry(intCountryID, intLanguageID, _
                                  strOptions, strListItems)
		
	''mark selected ID option
	strOptions = Replace(strOptions, _
										("""" & Trim(SelectedID) & """"), _
										("""" & Trim(SelectedID) & """ SELECTED") )
	list_CityOptionsForCountry = strOptions
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
This cries for some Ajax.
Eg like this:

(index.asp)
Code:
<html>

<script language="javascript" type="text/javascript">
  
    function CityList(countryid){
      var url="city.asp?c=" + countryid ;
      if (window.XMLHttpRequest) { 
           http_request = new XMLHttpRequest();
       } else if (window.ActiveXObject) { 
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
       }
       http_request.onreadystatechange = alertContents;
       http_request.open('GET', url, true);
       http_request.send(null);
   } 

    
    function alertContents() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               document.getElementById('citylist').innerHTML = http_request.responseText;
           } else {
               document.getElementById('citylist').innerHTML = "(Pick a country)";
           }
       }

   }
   

</script>


<form name="testform" method="POST" action="index.asp">

Country:
<select name="fCountry" onchange="CityList(this.value);">
<option value="NL">Nederland</option>
<option value="USA">USA</option>
</select>
<br />
City:
<div id=citylist></div>


<input type=submit value=submit>
</form>

</body>
</html>


(of course you'll pull the countries from the database


city.asp:
Code:
<select>
<%
dim cCountryId
cCountryId = request.QueryString("c")
if cCountryId = "NL" then
 response.Write "<option value=AMS>Amsterdam</option>" &_
                "<option value=RDM>Rotterdam</option>" 
else  ' USA
 response.Write "<option value=NY>New York</option>" & _
                "<option value=LA>Los Angeles</option>" 
end if
%>
</select>


and of course you'll retrieve the cities from a table with a query based on the incoming country_id (this is just a q&d example)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top