The code

admin

Administrator
Staff member
Following several replies asking me to provide a sample of the code I mentionedin my previous e-mail, I've decided to post it here. Please note that inmy project I only had to deal with two countries -- USA and Canada -- butyou can easily adapt this code to pull the list of countries from the database.Obviously, your JS array of states/provinces can get pretty big, but theoverall overhead of this solution will still be far less than that of anythingbased on XML.Good luck!<%@ Language=VBScript %><%Option ExplicitDim grsObj, gsStateIDs, gsStateNames, gsCountryIDs, giCount%><html><head><title>Vendor Information</title><script language="JavaScript"><%'Read states/provinces information into JavaScript arraysSet grsObj = Server.CreateObject("ADODB.Recordset")With grsObj.ActiveConnection = Application("DB").Source = "SELECT StateProvID, StateProvName, CountryID " + _"FROM StatesProvinces " + _"ORDER BY CountryID, StateProvName".OpenEnd WithgsStateIDs = "''"gsStateNames = "''"gsCountryIDs = "''"giCount = 1Do While Not grsObj.EOFgsStateIDs = gsStateIDs + ",'" + grsObj(0) + "'"gsStateNames = gsStateNames + ",'" + grsObj(1) + "'"gsCountryIDs = gsCountryIDs + ",'" + grsObj(2) + "'"giCount = giCount + 1grsObj.MoveNextLoopgrsObj.CloseSet grsObj = Nothing%>var arrayStateIDs = Array(<%=gsStateIDs%>);var arrayStateNames = Array(<%=gsStateNames%>);var arrayCountryIDs = Array(<%=gsCountryIDs%>);</script></head><body>.... other stuff on the page ....<form name="VendorInfo" action="vendorinfo.asp" method="post" onsubmit="returnvalidateVendorInfo()">.... other form controls ....Country:<select name="VendorCountry" onchange="showStates(this.options[this.selectedIndex].value)"><option value>select country<option value=http://forums.devx.com/archive/index.php/"US">United States<option value="CA">Canada</select>&nbsp;&nbsp;&nbsp;&nbsp;State/Province:<select name="VendorStateProv"><option value>select state/province</select></form><script language="JavaScript">// dynamically populate the list of states/provinces based on the choiceof countryfunction showStates(countryID){var pos = 0;with (document.VendorInfo.VendorStateProv){// replace existing state/province list entries with new onesfor (i = 1; i < <%=giCount%>; i++)if (arrayCountryIDs == countryID){pos ++max = options.length;if (pos < max){options[pos].value = arrayStateIDs;options[pos].text = arrayStateNames;}elseoptions[pos] = new Option(arrayStateNames, arrayStateIDs);};// delete all unused list entries at the end of the listmax = options.length;for (i = max-1; i > pos; i--)options = null;}}</script></body></html>
 
Back
Top