Problem in getting the checkbox state
Hi..
I have a JSP file which connects to DB and dispaly in aTable format by putting a checkbox as a first column of every row dispay. I am giving a name to every checkbox but its not a static name , its a name given dynamically by incrementing a varible in the process of looping(while)The checkbox names are
chbF0,chbF1,chbF2,.......
As the browser won't send the checkbox stauts which is not checked(for that it is throw NullPointerException,i verified), I m using hidden filelds. i m giving the name to them as chbSetState0,chbSetState1,chbSetState2,.... i m giving Initial values for these are as 0(zeroes).
I m going to set corresponding hidden value to 1,when the user clicks a checkbox. For that i m calling the function .. setState().
To set a hidden value to 1, i have to get the index name of the checkbox
that the user checked,that means i have to append that index value(index_chbSelected) to the hidden field name in this way....
// getting the index of check box clicked
var index_chbSelected=window.event.srcElement.name.charAt(4);
// setting the corresponding hidden value
(chbSetState+index_chbSelected).value=1;
But its an error javascript.
The code i written for his Jsp page is:
<%@ page import="java.*,java.sql.*,javax.*"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
<head>
<title>Book Edit page</title>
<script>
function setState()
{
var temp=window.event.srcElement.name.charAt(4);
if(BookForm.chbF<%=i%>.status)
{
var index_chbSelected=window.event.srcElement.name.charAt(4);
BookForm.(chbSetState+index_chbSelected).value="1"; //getting error here
}
else
{
BookForm.(chbSetState+index_chbSelected).value="0";
}
}
</script>
</head>
<body>
<form name="BookForm" method="get" action="BookSave">
<%!
Connection con=null;
Statement st=null;
ResultSet rs=null;
int i=0;%>
<%try
{
Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Reddy","root","admin");
st=con.createStatement();
rs=st.executeQuery("select * from bookdb");%>
<table border="1">
<tr><th><input type="checkbox"><th>Author</th><th>Title</th><th>Available</th></tr>
<%
while(rs.next())
{%>
<tr>
<td><input type="checkbox" name="chbF<%=i%>" onclick="setState()"><input type=hidden name="rowKey<%=i%>" value="<%=rs.getString(1) %>">
<input type="hidden" name="chbSetState<%=i%>" value="0"></td>
<td><input type=text name="txtAuthor<%=i%>" value="<%=rs.getString(2)%>"></td>
<td><input type=text name="txtTitle<%=i%>" value="<%=rs.getString(3)%>"></td>
<% String s=rs.getString(4);
if(s.equals("true"))
{%>
<td><input type="checkbox" checked name="chbAvail<%=i%>" value="on"></td>
<%}
else
{%>
<td><input type="checkbox" name="chbAvail<%=i%>" value=""></td>
<%}
i=i+1; %>
</tr>
<%}%>
</table><br><br>
<input type=hidden name="rowCnt" value="<%=i %>">
����������<input type="submit" value="Save">
<%}
catch(Exception se)
{%> <%= se %>
<%}%>
</form>
</body>
</html>
This is the logic i written to know the other page(jsp page will get the hidden field values.If a hidden field value is 1,it treats check box selected)get checkbox state of this page.
I tried so much to get solution for this.
If any one understood my problem, pls help me to solve this problem.