Click to See Complete Forum and Search --> : drop down list problem


bathulasuman
11-01-2006, 02:29 AM
hi friends
i have prob with country n states list populating dynamically.
when i select country in the 1st drop down list box , corresponding states should be populated in the 2nd drop down box.
i am able to populate country list in the 1st drop down list box successfully.
problem is with states list, all the states of all the countries are being populated irrespective of the country selected
(say i selected country "india", then i should get only indian states, but 2nd drop down list box is listing all the states of all the countries).
some condition has to b checked b4 listing states list n i am not able to do so, can any one help me in this issue

i took array for countries and the haspmap for states

here is my code
-------------------------------------------------

<HTML>
<%@ page import="java.sql.*,
java.util.*" %>
<%
String key = "";
String key1 = "";
Connection con = null;
Statement stm = null;
ResultSet rs = null;
ResultSet rs1 = null;
PreparedStatement pstm = null;
java.util.ArrayList arr = new java.util.ArrayList();
java.util.ArrayList arr1 = new java.util.ArrayList();
java.util.ArrayList arr2 = new java.util.ArrayList();
java.util.HashMap hmap = new HashMap();
String query = "SELECT CID,CNAME FROM COUNTRYINFO";
String query1 = "SELECT SNAME FROM STATEINFO WHERE CID = ? ";
String id = "";
Iterator itr = null;
String temp ="";
%>
<HEAD>
<TITLE> JDBC </TITLE>
<script language="javascript">
function loadValue(id)
{
alert(id.value);

<%
itr = hmap.keySet().iterator();
while(itr.hasNext())
{
key = (String)itr.next();
out.println(key);
}
%>
}
</script>
</HEAD>

<BODY >
<form name = "frm">
<%@ page import="java.sql.*,
java.util.*" %>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:mothidsn");
stm = con.createStatement();
rs = stm.executeQuery(query);
pstm = con.prepareStatement(query1);
while(rs.next())
{
id = rs.getString(1);
arr1 = new ArrayList();
arr.add(id+"&"+rs.getString(2));
pstm.setString(1,id);
rs1 = pstm.executeQuery();
while(rs1.next())
{
arr1.add(rs1.getString(1));
}
hmap.put(id,arr1);
}

%>
<select name="deptname" onChange="loadValue(this)">
<%
StringTokenizer str = null;
for(int i=0;i<arr.size();i++)
{
str = new StringTokenizer((String)arr.get(i),"&");
if(str.hasMoreTokens()){
key = str.nextToken();
%> <option value="<%=key%>"><%=str.nextToken()%></option>
<% }
}
%>
</select><br>

<select name="deptdet">
<%
itr = hmap.keySet().iterator();
while(itr.hasNext())
{
key1 = (String)itr.next();
arr2 = (ArrayList) hmap.get(key1);
for(int z=0;z<arr2.size();z++)
{
%>
<option value="<%= arr2.get(z)%>"><%= arr2.get(z)%></option>
<%
}
}
%>
</select>
<% }
catch(Exception e)
{
System.out.println("ERROR "+e);
}

%>
</form>
</BODY>
</HTML>
------------------------------------------

if u know any other way, plz let me know.

plz help me

waiting 4 the reply

thank u in advance
regards
suman