create a dropdown list based on selection from another dropdown list in jsp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bijiann
    New Member
    • May 2015
    • 4

    create a dropdown list based on selection from another dropdown list in jsp

    I am doing a program in java .I have designed a java server page which has 3 drop down list's...countr y,state.I want to display only corresponding the values in drop down list state when a country is selected...(for example:-if India is selected,drop down list state must have only the name of states in India).I have created 2 tables country and state.Table country has country id and country name.Table state has state id,state name and a foreign key country id from table country.
    Code:
    String coun=request.getParameter("countryddl");[CODE]
    ResultSet rs1=stmt.execut eQuery("select * from country where countryname='"+ coun+"'");
    while(rs1.next( ))
    {
    Integer counid=Integer. parseInt(rs1.ge tString("countr yid"));
    ResultSet rs= stmt.executeQue ry("select * from state where countryid="+cou nid);
    %> <select name="stateddl" >
    <option value="0">Selec t</option>
    <% while(rs.next() )
    {
    %>
    <option><%= rs.getString("s tatename")%></option>

    <% } %>
    </select>
    <% }
    }
    [/CODE]
    Full Code is attached with this message.

    Please help me to complete this program...
    Attached Files
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    It is some years ago when I did exactly the same, so let me see what I remember.
    First, do not fill the drop down boxes in JSP as what I see in your code above. Just create a two-dimensional JSON array in Javascript instead, like:
    Code:
    ["country1":{state1, state1}, "country2":{state3, state4, stae5}]
    Then create the website with empty dop-down-lists for state and country and call a javascript method setCountry("cou ntry1") in the onLoad() that fills the drop-down-boxes with values from the JSON array. You should call the setCountry method also every time you select a new country from the drop-down box, so that the second box will be updated accordingly and only contain the states from the selected country.

    Comment

    • Bijiann
      New Member
      • May 2015
      • 4

      #3
      Thank you for replying...But my problem is not solved.I want to get the drop down list populated from the tables country and state in data base.I don't know java script.How can i do this?Can you Please provide code in java script.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Maybe you should read about JSON and javascript. Then you will see that my email above exactly solves your problem. I mean, you don't read from database and produce HTML directly, but instead you read from database and produce JSON. Then you use Javascript to make HTML out of JSON.

        But no need to reinvent the wheel, there is some javascript ready for you to plug it in.
        Just use "jquery". See at http://www.sitepoint.com/16-jquery-s...-down-plugins/
        Especially chapter "4. jQuery Chained Select".

        Side remark:
        You are lucky. In the past when I wrote my drop-down-box code for country and city, jquery did not even exist.

        Comment

        • Bijiann
          New Member
          • May 2015
          • 4

          #5
          chapter "4. jQuery Chained Select" is giving an error page in the above link...can you please give an example of the code.

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            The link is down somehow. Just google for it ("jQuery Chained Select") and you will find a lot of links that are working with much more demos and code examples. For example http://www.appelsiini.net/projects/chained

            Comment

            • Bijiann
              New Member
              • May 2015
              • 4

              #7
              In the example in this link, values are directly inserted to drop down list.I want to insert values into drop down list from a table.Does it make any difference?

              Where should i place this code?in a java script??
              $("#series").ch ained("#mark");
              I have placed it in a java script.But have not got the output.
              <script type="text/javascript">
              $("#series").ch ained("#mark");
              </script>
              I don't know ajax and java script.Please provide code in detail...

              Comment

              Working...