SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question ASP/JavaScript dependent dropdown menus??

    ok guys, here's my first post... hopefully my question isnt too noobish.

    i'm trying to create a dependent dropdown menu based on two objects, packages and steps (the steps dropdown will be dynamically updated/populated when a package is selected). the site uses ASP and since ASP is server-side, i am using JavaScript to handle the client-side "dynamic" change. i've found several snippets of code that create the dependent dropdown; however, the two examples i have use arrays which are statically defined. the data that will populate the dropdowns is coming from a SQL database, and that data will be updated/changed fairly often. my question is this: is there any way to generate the arrays using ASP (outside javascript tags) and use them in the javascript? or is there a way to use javascript to generate the correct arrays and fill them with data from my SQL tables? can i use a recordset ojbect in javascript? i have heard that you may use PHP inside javascript tags, but i'm unsure if that is what i want to do.. especially since i am using ASP as the main language for this site. any help would be appreciated. THANKS!


    below is the javascript i found online. i would like arrStates, arrFlorida, arrNewYork, arrTennessee to be generated by the database. for my purposes, each state is synonymous with a package and cities = steps.

    Code:
    <script type="text/javascript">
    // Create arrays containing state cities
    // Note: These arrays could be created dynamically
    var arrPackages, arrFlorida, arrNewYork, arrTennessee	
    arrFlorida = ["Daytona Beach", "Miami", "Orlando", "Tampa"]
    arrNewYork = ["Albany","New York", "Oakland", "Rochester"]
    arrTennessee = ["Gatlinburg", "Knoxville", "Memphis", "Nashville"]
    arrStates =[arrFlorida, arrNewYork, arrTennessee]
    
    // Function to handle dynamically altering the contents of the City List box
    function handleChange(newDisplay)
    {   var StateSelect, CitySelect, NumEntries, i
        StateSelect = document.frm.States
        CitySelect = document.frm.Cities
    
      // Delete all entries in the cities list box
      for (i = CitySelect.length; i > 0; i--)
      {
        CitySelect.options[i-1] = null
      }
    
      // Add comment option to City List box
      CitySelect.options[0] = new Option("-- Select City --",0)
    
      // If state is selected add its cities to the City List box
      if (newDisplay >= 0)
      {
        NumEntries = arrStates[newDisplay].length
        for (i = 1; i <= NumEntries; i++)
        {
          CitySelect.options[i] = new Option((arrStates[newDisplay])[i-1],(i))
        }
      }
      CitySelect.selectedIndex = 0
    
    }
    </script>
    and the actual select's in the form:
    Code:
    			<td>
    			States:
    			<select name="States" onChange="handleChange(this[this.selectedIndex].value)">
    			  <option value="-1">-- Select State --
    			  <option value="0">Florida
    			  <option value="1">New York
    			  <option value="2">Tennessee
    			</select>
    			<p>
    			
    			Cities: 
    			  <select name="Cities">
                    <option value="-1">-- Select City --
                    <option>
                    <option>
                    <option>
                    <option>
                                </select>
    			<script type="text/javascript">
    			handleChange(-1)
    			</script>
    			</td>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure you can do that.

    Instead of
    var arrPackages, arrFlorida, arrNewYork, arrTennessee
    arrFlorida = ["Daytona Beach", "Miami", "Orlando", "Tampa"]
    arrNewYork = ["Albany","New York", "Oakland", "Rochester"]
    arrTennessee = ["Gatlinburg", "Knoxville", "Memphis", "Nashville"]
    arrStates =[arrFlorida, arrNewYork, arrTennessee]

    You could do something like (100% untested but hey it's the idea that counts) :
    Code:
    <%
    SQL = "SELECT State, City FROM Table ORDER BY State ASC, City ASC"
    RS = DB.Execute(SQL)
    
    sState = ""
    sCities = ""
    Do While Not RS.EOF
    	If RS.Fields("State") <> sState Then
    		If sState <> "" Then
    			sHTML = sHTML & "var arr" & sState & " = [" & Left(sCities,Len(sCities) - 1) & "];" & vbCrLf
    		End If
    		sState = RS.Fields("State")
    		sCities = ""
    	End If
    	sCities = sCities & """" & RS.Fields("City") & ""","
    	RS.MoveNext
    Loop
    sHTML = sHTML & "arr" & sState & " = [" & Left(sCities,Len(sCities) - 1) & "];" & vbCrLf
    
    Response.Write sHTML
    %>

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks jim, i'll give it a spin and see if it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •