Reputation: 616
I need to fill the dropdownlist according to selected field in another dropdownlist.How can i implement this? Here is my code :
<tr>
<td>CITY :</td>
<td><select name="city">
<%
while (rs.next()) {
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select></td>
</tr>
<%
rs = db.runQuery(conn, "Select distinct town from adress where city='" + request.getParameter("city") +"'");
%>
<tr>
<td>TOWN</td>
<td><select name="town">
<%
while (rs.next()) {
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
<tr>
First dropdown fills correctly but second one does not work.How can i handle this?
Upvotes: 2
Views: 2420
Reputation: 4369
The reason why you cannot do it is that JSP compiles to Servlet and runs on server side. The Servlet runs only once before you select a city. I think the easiest solution is submitting the form twice:
<select name="city" onChange="this.form.submit();">
The nicer solution is using Ajax on client side, but that needs more JavaScript or some JavaScript libraries or JSP taglibs. jQuery is a pretty useful JavaScript framework for such tasks.
Upvotes: 2