Hi,
If you are planning to use javascript.Please see the code below.
<html>
<body>
<form name="form1">
Select a City:
<select name="city" onchange="changePlaces()">
<option value="vijaywada">Vijaywada
<option value="tenali">Tenali
</select>
Select a Place:
<select name="places">
</select>
</form>
</body>
</html>
<script>
function changePlaces() {
var selectedCity=document.form1.city.options[document.form1.city.selectedIndex].value;
if(selectedCity=="tenali") {
option1 = new Option("gov","governorpet");
option2 = new Option("besant","besant road");
document.form1.places.options[0] = option1;
document.form1.places.options[1] = option2;
}
else {
option1 = new Option("bose","Bose Road");
option2 = new Option("ratna","Ratna Theatre");
document.form1.places.options[0] = option1;
document.form1.places.options[1] = option2;
}
}
</script>
Simply use a javascipt funtion and acll this function in onchange wvent of list box.
example function is given below:-
function selSkillType()
{if(document.form1.cmbSkillType.value=='')
{}
else
{document.form1.action="firstForm.jsp?index1="+document.form1.cmbSkillType.value;
document.form1.submit();
}
}
when you call this function it will submit to the same page.but that time its url has the id of selected list box value.
using request.getParameter method u can access that value.with that value you can execute any sql query.
but the problem is when you load the page first time there is no variable in the url.so y have to check this with a strong if condition.
i think this will give you a small idea.
if you don't get any thing ,i can give you some demo jsp pages .please inform me
thank you
<label>
<select name="party" size="1" onChange="document.forms[0].elements['city'].selectedIndex =this.selectedIndex">
<option >Select</option >
<%while(rs1.next()){%>
<option value="<% out.write(rs1.getString("name1")); %>" >
<% out.write(rs1.getString("name1")); %>
</option>
<option value="<% out.write(rs1.getString("name2")); %>" >
<% out.write(rs1.getString("name2")); %>
</option>
<%}%>
</select>
</label>
<strong> </strong> </td>
<td width="440"><strong>City:
<label>
<select name="city" size="1" style="class2" >
<option>Select</option>
<span class="style3">
<%
String db1 ="SELECT city1,city2,id From listing";
ResultSet rs2 =statement.executeQuery(db1);
%>
<%while(rs2.next()){%>
<option value="<% out.write(rs2.getString("city1")); %>" >
<% out.write(rs2.getString("city1")); %>
</option>
<option value="<% out.write(rs2.getString("city2")); %>" >
<% out.write(rs2.getString("city2")); %>
</option>
<%}%>
</select>
</span>
</label>
here is the code for changing the content of combo box dynamically taking data from data base with out submit.
this uses string buffer in jsp page and pass it to a function in java script.
<%
ArrayList statuslist = (ArrayList)request.getSession().getAttribute("alleqpt");
StringBuffer sb = new StringBuffer();
String[] str1 = new String[statuslist.size()] ;
String[] str2 = new String[statuslist.size()] ;
/// this is to put it in a string buffer
Iterator it=statuslist.iterator();
System.out.println(statuslist.size());
for(int i=0;i<statuslist.size();i++)
{
String[] str = (String[])it.next();
str1 = str[0];
str2= str[1];
sb.append("'"+str2+"'"+",'"+str1+"',");
}
sb.deleteCharAt(sb.length()-1);
System.out.println(sb);
%>
<script language="javascript" type="">
function getEqpttype()
{
var x = document.getElementById('eqptlst');
var y = x.value;
var tryonce = document.getElementById('eqpttypelst');
tryonce.length = 0;
var array = new Array(<%=sb%>)
for(var i=0;i<array.length;i++)
{
if(y==array[i+1])
{
tryonce.options[tryonce.length] = new Option(array)
}
}
}
></script>