Hi All,
I written a code for one scenario,
let me explain that...
i created a jsp page with two combo boxes.
in first combo box i took values from data base using jdbc imagine
those values are
India
America
if i select India in first combo box it should show the states of india in second combo box...
if i select America it should show states of America.
i written code using script for that selection like
here d is selected value in first combo box.
like this i put conditions for those two values i.e India,America..
but if two more countries and also their states will add in the future then my code will not work. then how can i solve it.
anybody can help me
i want to update it automatically
Here is a brute force approach using javascript
:
Put a hidden select on the page. On load of the page, populate this select with all the Countries and States that you have in the database in a systematic manner. An example is
XXAmerica
city1
city2
.
.
.
cityn
XXIndia
city1
city2
.
.
.
cityn
So that you have a label (XX in this case) and a country followed by all the cities of that country for all the countries that you have in the database.
This is your dataselect and you don't show this on the page (style = "display:none"). Call this, say dataSelect.
Then you have your two drop downs. One for countries and the other for cities.
You load the countries drop down with all the countries you have and put an onchange on it. So onchange of the countries dropdown, you call a method which does the following
take the selected country, add the label (XX for this example) and search for this value in the dataselect which you can access using javascript.
Once you find the entry there, retrieve all the values from the select that follow after it until you reach a value that starts with the label (XX). As you retrieve each value, add them as options to the cities select.
The code for this would look something like
[HTML]
function populateCities(form) {
var all = form.dataSelect.options.length;
form.citiesSelect.options.length = 0; //clear all cities
var country = form.countries.options[form.countries.selectedIndex].value;//selected country
var end = false;
var temp = 0;
for (i = 0;(i < all) && (end == false); i++) {
var current = form.dataSelect.options[i].value;
if(current == "XX" + country ) {//find country in dataSelect
index = i + 1;
var cur = form.dataSelect.options[index].value ;
var cursub = cur.substring(0, 2);
while(cursub != "XX") {
var nextValue = form.dataSelect.options[index].value;
var nextText = form.dataSelect.options[index].text;
form.citiesSelect.options[temp++] = new Option(nextText, nextValue);
index = index + 1;
cur = form.dataSelect.options[index].value;
cursub = cur.substring(0, cuntry.length);
}
end = true;
}
}
}
[/HTML]
As you can see this is purely a Javascript problem so I'll copy it there.