populating a drop down using another drop down

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spooky
    New Member
    • Oct 2006
    • 27

    populating a drop down using another drop down

    hii guyzz
    Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
  • ggosavi
    New Member
    • Oct 2006
    • 2

    #2
    Originally posted by spooky
    hii guyzz
    Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
    Hi,

    You can try this code snippet. I may be helpful.

    Code:
    <html>
    <head>
    <title>Country Selection</title>
    <script language="javascript">
    var Country = null;
    function init()
    {
    	var numCountry = 2;
    	var oStateSlct = document.CountrySelect.Function;
    	Country = new Array(numCountry+1); 
    	oStateSlct.options[0] = new Option("Please select a Country Type", "-1");
    	Country[0] = new Array(0); 
    	oStateSlct.options[1] = new Option("Country1", "1");
    	Country[1] = new Array(1);
    	Country[1][0] = new Option("State1", "-1");
    	oStateSlct.options[2] = new Option("Country2", "2");
    	Country[2] = new Array(2);
    	Country[2][0] = new Option("State1", "-1");
    	Country[2][1] = new Option("State2", "0");
    			
    }
    
    function fillUpSelectControl(srcCntrl,targetCntrl,selectOptions)
    {
    	var slctIndx = srcCntrl.options.selectedIndex;
    	for( i=0; i<targetCntrl.options.length; i++ )
    		targetCntrl.options[i] = null;
    
    	var newOptions = selectOptions[slctIndx]; 
    	for( i=0; i<newOptions.length; i++ )
    		targetCntrl.options[i] = new Option( newOptions[i].text, newOptions[i].value );
    	targetCntrl.options[0].selected = true;
    }
    </script>
    </head>
    <body onload="init();">
    
    <form name="CountrySelect" >
    <select NAME="Function" onChange="fillUpSelectControl(this,this.form.StateType,Country)">
    </select>
    <select name="StateType">
    </select>
    </form>				
    				
    </body> 
    </html>

    Comment

    • spooky
      New Member
      • Oct 2006
      • 27

      #3
      hii
      thanks for the snippet , i've been doin this project using Servlets,JSP and JDBC.
      I dont know much about scripting how can i connect to db using this
      anil

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        You would probably have to use AJAX, DOM you can find help for that all over google good examples.

        Comment

        • spooky
          New Member
          • Oct 2006
          • 27

          #5
          Check this code will it work...

          Will this code work if yes can i get the value of hidden Name to my jsp page!!


          function madeSelection(s elObj)
          {
          var selectedValue = selObj.options[selObj.selected Index].value;
          //document.Name.v alue = "ANIL";
          document.hidden Name.value = selectedValue;
          document.write( document.hidden Name.value);
          }
          html tag------
          input type=hidden name=hiddenName

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by spooky
            Will this code work if yes can i get the value of hidden Name to my jsp page!!


            function madeSelection(s elObj)
            {
            var selectedValue = selObj.options[selObj.selected Index].value;
            //document.Name.v alue = "ANIL";
            document.hidden Name.value = selectedValue;
            document.write( document.hidden Name.value);
            }
            html tag------
            input type=hidden name=hiddenName
            No. This will not work.
            The culprit line is
            Code:
             document.hiddenName.value = selectedValue;
            which should be
            Code:
             document.formName.hiddenName.value = selectedValue;
            or
            Code:
             
            document.forms['formName'].hiddenName.value = selectedValue;
            Where formName is the name of the form on which the hidden input is.
            Yes you can get to this value in a jsp using
            Code:
             String name = request.getParameter("hiddenName");

            Comment

            • SandeepKumar2k6
              New Member
              • Sep 2010
              • 1

              #7
              how can we take the values from mysql databases ,

              Hi ,
              ur code is really excellent. can u just give me
              the code by taking the values of countries and states from the mysql db. is it possible to take the values from the db..?(mysql and jsp). can pls reply back immediately.... thanks in advance..

              Comment

              Working...