Populate a second drop down based on a selection from another above it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mimsc
    New Member
    • Sep 2007
    • 14

    Populate a second drop down based on a selection from another above it

    Based off a selection from an above drop-down on the same page?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by mimsc
    Based off a selection from an above drop-down on the same page?
    Yes it's possible.
    Are you talking about a Java application or an web application with HTML and Javascript?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by mimsc
      Based off a selection from an above drop-down on the same page?
      Sure, why not? Instantiate a new PopupMenu and populate it with whatever you
      want and display the thing. What's the problem? Or are you talking Javascript?

      kind regards,

      Jos

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        r035198x is catching up: he beat me by that >.< much ;-)

        kind regards,

        Jos

        Comment

        • mimsc
          New Member
          • Sep 2007
          • 14

          #5
          Originally posted by r035198x
          Yes it's possible.
          Are you talking about a Java application or an web application with HTML and Javascript?
          It's a web app with some html, unfortunately I'm new to javascript and I hear thats the way to go with this problem.

          here's what I got:

          Code:
          <%
          Vector theStates = WeatherDAO.getWeatherStates();
          %>
          
          label for="weatherStat">State:</label>
          
          <select name="weatherStat" id="States">
          <% for (int i=0; i< theStates.size(); i++) {  %>
          
          <OPTION VALUE=<%=theStates%>> <%=theStates.elementAt(i)%> </OPTION>
          
          <% 
          }
          %>
          </select>		
          
          <label for="weatherCity">City:</label>
          	<select name="weatherCity" id="Cities">
           	
           
          <%
          Vector theCities = WeatherDAO.getWeatherCities(WeatherStat);
          %>	
          
          <% for (int i=0; i< theCities.size(); i++) {  %>
          
          <OPTION VALUE=<%=theCities%>> <%=theCities.elementAt(i)%> </OPTION>
          
          <% 
          }
          %>
          
          
          </TD></TR>
          </center>
          <TR><TD> 
          <br>
          <br>
          <center>
          
          
          
          
          
          						
          </select></TD></TR>
          </TABLE>
          nothing populates in the second drop-down

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by mimsc
            It's a web app with some html, unfortunately I'm new to javascript and I hear thats the way to go with this problem.

            here's what I got:

            Code:
            <%
            Vector theStates = WeatherDAO.getWeatherStates();
            %>
            
            label for="weatherStat">State:</label>
            
            <select name="weatherStat" id="States">
            <% for (int i=0; i< theStates.size(); i++) {  %>
            
            <OPTION VALUE=<%=theStates%>> <%=theStates.elementAt(i)%> </OPTION>
            
            <% 
            }
            %>
            </select>		
            
            <label for="weatherCity">City:</label>
            	<select name="weatherCity" id="Cities">
             	
             
            <%
            Vector theCities = WeatherDAO.getWeatherCities(WeatherStat);
            %>	
            
            <% for (int i=0; i< theCities.size(); i++) {  %>
            
            <OPTION VALUE=<%=theCities%>> <%=theCities.elementAt(i)%> </OPTION>
            
            <% 
            }
            %>
            
            
            </TD></TR>
            </center>
            <TR><TD> 
            <br>
            <br>
            <center>
            
            
            
            
            
            						
            </select></TD></TR>
            </TABLE>
            nothing populates in the second drop-down
            1.) Vector is old. Use ArrayList
            2.) If the cities are coming from a database as well then you probably may have to do it using Ajax or else you load all cities and states into arrays and then play around with those arrays. Either way it's a web problem so I'll move this to the Javascript forum.

            P.S This problem appears so often that if you do a search on it you'll find a solution in the Javascript forum

            Comment

            • mimsc
              New Member
              • Sep 2007
              • 14

              #7
              Originally posted by r035198x
              1.) Vector is old. Use ArrayList
              2.) If the cities are coming from a database as well then you probably may have to do it using Ajax or else you load all cities and states into arrays and then play around with those arrays. Either way it's a web problem so I'll move this to the Javascript forum.

              P.S This problem appears so often that if you do a search on it you'll find a solution in the Javascript forum

              thanx for the input...appreci ate it

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                See if this link helps.

                Comment

                • mimsc
                  New Member
                  • Sep 2007
                  • 14

                  #9
                  Originally posted by acoder
                  See if this link helps.

                  hey thanx man, Im about to look at it right now!

                  Comment

                  • mimsc
                    New Member
                    • Sep 2007
                    • 14

                    #10
                    Need help in passing a value

                    ok so I think I got the javascript syntax down...how can I access the saved parameter inside the script to be used in a query to populate a "city" drop-down menu below

                    example below:

                    [code=html]
                    <%
                    Vector theStates = WeatherDAO.getW eatherStates();
                    %>


                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                    <title>Insert title here</title>
                    <script language="JavaS cript"
                    type="text/JavaScript">
                    function GetCities(testV ar)
                    {
                    var weatherState = document.testFo rm.weatherStat. selectedIndex; <<----- I need to use this value to create another drop down menu on this same page


                    }
                    </script>

                    <body>




                    <TR bgColor="#fffff f"><TD COLSPAN="2">
                    <TABLE CELLPADDING="3" CELLSPACING="2" >
                    <TR><TD>

                    </FONT></CENTER><BR>
                    <BR>
                    <TABLE BORDER="0" CELLSPACING="5" CELLPADDING="5" WIDTH="100%">
                    <TR BGCOLOR="#C0C0C 0" ><TD COLSPAN="2" ALIGN="CENTER"> <FONT SIZE=+3><B> Weather Services</B></FONT>
                    </TD></TR>
                    </TABLE>
                    <HR SIZE="3" NOSHADE>
                    <font size=+1>Select the State and City for which you would like to receive weather information.</font><br>
                    <TR><TD>
                    <center>

                    <form name="testForm" >
                    <label for="weatherSta t">State:</label>

                    <select name="weatherSt at" onchange="testV ar = this.value">

                    <% for (int i=0; i< theStates.size( ); i++) { %>

                    <OPTION VALUE=<%=theSta tes%>> <%=theStates.el ementAt(i)%> </OPTION>

                    <%
                    }
                    %>
                    </select>

                    <label for="weatherCit y">City:</label>
                    <select name="weatherCi ty" id="Cities">


                    [/code]



                    Im lost...any help would be appreciated

                    Comment

                    • iam_clint
                      Recognized Expert Top Contributor
                      • Jul 2006
                      • 1207

                      #11
                      not sure what your asking can you try to explain alittle more

                      Comment

                      • mimsc
                        New Member
                        • Sep 2007
                        • 14

                        #12
                        Originally posted by iam_clint
                        not sure what your asking can you try to explain alittle more

                        I need to grab the selected value from "weatherSta t" select box and use it in another dop down menu below called "weatherCity".. ...the only catch is the cites have to be populated by a query that's in:

                        Vector theStates = WeatherDAO.getW eatherStates(th is is where the captured value needs to go)


                        Im new to javascript

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Merged your threads - they were on the same topic.

                          Also changed the thread title to describe the problem a little better.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by mimsc
                            I need to grab the selected value from "weatherSta t" select box and use it in another dop down menu below called "weatherCity".. ...the only catch is the cites have to be populated by a query that's in:

                            Vector theStates = WeatherDAO.getW eatherStates(th is is where the captured value needs to go)
                            You have two choices. You can either use Ajax where you pass the selected value onto the server-side script which returns the response which you can parse to populate the second dropdown. The second option is to use arrays to store the option name/values ready for use as shown in the example link. You can generate the options as and when they are needed. Which method would you prefer?

                            Comment

                            • mimsc
                              New Member
                              • Sep 2007
                              • 14

                              #15
                              Originally posted by acoder
                              You have two choices. You can either use Ajax where you pass the selected value onto the server-side script which returns the response which you can parse to populate the second dropdown. The second option is to use arrays to store the option name/values ready for use as shown in the example link. You can generate the options as and when they are needed. Which method would you prefer?

                              the arrays sound too cumbersome, especially with cities for all 50 states....how does that AJAX work?

                              Comment

                              Working...