aspose file tools*
The moose likes JSP and the fly likes onChange in dropdown. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "onChange in dropdown." Watch "onChange in dropdown." New topic
Author

onChange in dropdown.

Arun Vethadas .T
Ranch Hand

Joined: Dec 13, 2001
Posts: 36
hai
i have two dropdown menu(combo box) in a jsp page.i getting value for the menu from the database. i want when i select first menu
onchange i have to cahnge second menu from the database. based on first select value i have to select from database. is it possible. pls.
give me some sample code,using javascript and JSP.
tables are in Oracle Table. when i select first menu value automaically according to the value second menu value will change (loading from database. (eg. country and state)
when i select india ,indian states are avail in next menu. when i select US , us state will display in the combo box.
pls. help me soon.
Thanks,
Arun.
Hari Haran
Greenhorn

Joined: Sep 21, 2000
Posts: 19
Hi Arun,
I too struggled for this and I have somehow managed to do it with some difficulty.
So use it and save ur time.I am sending the code.
In my case : The country and state are stored in SQL.
mgr.getListSorted("Country_Key")will fetch
Country Key : IN
Country Value : India
mgr.getSub_List("State") will fetch the following from the table
State Key :IN-TN
State Value :Tamilnadu
<tr>
<td width="30%"><b>Country key *</b></td>
<td width="70%">
<!-------------------------------Country-State Code Starts Here -------------------------->
<%

//Get the Country List
TreeMap tm_Country = mgr.getListSorted("Country_Key");
Collection col_Country = tm_Country.values();
Set set_Country = tm_Country.keySet();
Iterator it_Country_Elements= col_Country.iterator();
Iterator it_Country_Keys= set_Country.iterator();
//Get the State List
Hashtable ht_State = mgr.getSub_List("State");
Enumeration enum_State_Elements = ht_State.elements();
Enumeration enum_State_Keys = ht_State.keys();
String key="";
String val="";
String statekey="";
String statevalue="";
String countrykey="";
%>

<select name="cmb_Country" onChange="val()">
<option value = "-1" selected>--Select a Country--</option>

<%

while(it_Country_Elements.hasNext() && it_Country_Keys.hasNext())
{
key = (String)it_Country_Keys.next();
val = (String)it_Country_Elements.next();
%>
<option value = "<%out.println(val);%>" ><%out.println(key);%></option>

<%
}
%>
</select>

<script language="javascript">
var Country_Code = new Array();
<%

while(it_Country_Keys.hasNext())
{
int ctr=0;
countrykey = (String)it_Country_Keys.next();
%>
Country_Code[<%=ctr%>]="<%=countrykey%>";
<%
ctr++;
}
%>
var State_Code = new Array();
var State_Desc = new Array();
var State_Code_Actual = new Array();
<%
int i=0;
String str_State_Code = null;
while(enum_State_Elements.hasMoreElements() && enum_State_Keys.hasMoreElements())
{
statekey = (String)enum_State_Keys.nextElement();
statevalue = (String)enum_State_Elements.nextElement();
StringTokenizer st = new StringTokenizer(statekey,"-");
while (st.hasMoreTokens())
{
str_State_Code = st.nextToken();
}
str_State_Code= str_State_Code.trim();
System.out.println(str_State_Code);
%>
State_Code[<%=i%>]="<%=statekey%>";
State_Desc[<%=i%>]="<%=statevalue%>";
State_Code_Actual[<%=i%>]="<%=str_State_Code%>";

<%
i++;
}
%>
</script>

<script language="JavaScript">
function val()
{

while(document.frm_Address_Create.cmb_State.options.length)
{
document.frm_Address_Create.cmb_State.options[0] = null;

}
document.frm_Address_Create.cmb_State.options[0] = new Option("Select a State","-1")
var country_value = document.frm_Address_Create.cmb_Country.value;
country_value = country_value.substring(0,2);
var k=1;

for(var l=0; l < State_Code.length;l++)
{
State_Code[l] = State_Code[l].substring(0,2);
if(country_value == State_Code[l])
{
document.frm_Address_Create.cmb_State.options[k] = new Option(State_Desc[l],State_Code_Actual[l]);
k++;
}
}
}
</script>

<!-------------------------------Country-State Code Ends Here -------------------------->
</td>
</tr>
<tr>
<td width="30%"><b>State *</b></td>
<td width="70%">

<select name="cmb_State">
<option value = "-1" selected>Select a State</option>
</select>
</td>
</tr>
Regards,
Hari
"Happiness is Perfect only when Shared"
Arun Vethadas .T
Ranch Hand

Joined: Dec 13, 2001
Posts: 36
Thanks Hari
Arun.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: onChange in dropdown.
 
Similar Threads
skip value in logic: iterate
drop down box using struts
How do I populate dynamic combox items
Combo box selection problem in jsp
how to populate dropdown box dynamically