• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

onChange in dropdown.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hari
Arun.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic