Fetch Records from Database after Selecting Dropdownlist

To fetch records from database after selecting dropdown list the associated records change in second dropdown list at runtime.

Solution 1:

You can use javascript to forward the page to a servlet/jsp/cgi(Hotmail registration style) which would populate/generate sub-list and forward back to the page with onselect event. This solution would depend on lot of other factors.

Consider the solution below:

Solution 2:

Better solution would be to do the same without the need to populate the list from the server (yahoo registration style)

Look at the following code

<html>
<head>
<script>
var regiondb = new Object( )
regiondb["kashmir"] = [{value:"102", text:"Srinagar"},
                       {value:"88", text:"Leh"},
                       {value:"80", text:"Pulvama"},
                       {value:"55", text:"More"}]; regiondb["up"] = [{value:"30", text:"Lucknow"},
                     {value:"21", text:"Kanpur"},
                     {value:"49", text:"Gorakhpur"},
                     {value:"76", text:"Meeruth"},
                     {value:"14", text:"More"}]; regiondb["maharashtra"] = [{value:"64", text:"Mumbai"},
                           {value:"12", text:"Pune"}]; </script> <script> function setCities(chooser) {
     var cityChooser = chooser.form.elements["city"];
     // empty previous settings
     cityChooser.options.length = 0;
     // get chosen value to act as index to regiondb hash table
     var choice = chooser.options[chooser.selectedIndex].value;
     var db = regiondb[choice];
     // insert default first item
     cityChooser.options[0] = new Option("Choose a City:", "", true, false);
     if (choice != "") {
      // loop through array of the hash table entry, and populate options
      for (var i = 0; i < db.length; i++) {
       cityChooser.options[i + 1] = new Option(db[i].text, db[i].value);
      }
  }
}

</script>
</head>
<body>
<form>
Submit Request to: <select name="continent" onchange="setCities(this)">
     <option value="" selected>Choose a State:</option>
     <option value="kashmir">Kashmir</option>
     <option value="up">UttaraPradesh</option>
     <option value="maharashtra">Maharashtra</option>
</select>&nbsp;
<select name="city">
     <option value="" selected>Choose a City:</option> </select> </form> </body> </html>

Java Tips by : Swivel

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.