anjugrao
asked on
Checkboxes & Arrays
Hi,
THis is my problem. I'm dynamically creating a series of checkboxes based on a user's profile (done through a sql query) shown below:
<TD><INPUT type="checkbox" name="cprId" value="10611583">TD>
<TD><INPUT type="checkbox" name="cprId" value="10611584"></TD>
<TD><INPUT type="checkbox" name="cprId" value="10611585"></TD>
i want to capture (on the next page) which of these checkboxes are selected and which are not based on their value. I know that I need to use an Array on the next page, but how do I do it? SHould I use request.getparameter() if so, what is the syntax? Any help would be greatly appreciated.
THis is my problem. I'm dynamically creating a series of checkboxes based on a user's profile (done through a sql query) shown below:
<TD><INPUT type="checkbox" name="cprId" value="10611583">TD>
<TD><INPUT type="checkbox" name="cprId" value="10611584"></TD>
<TD><INPUT type="checkbox" name="cprId" value="10611585"></TD>
i want to capture (on the next page) which of these checkboxes are selected and which are not based on their value. I know that I need to use an Array on the next page, but how do I do it? SHould I use request.getparameter() if so, what is the syntax? Any help would be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i forgot one closing bracket at the end.
Pellep method only capture which of the checkboxes are selected. But, it can't capture which of the checkboxes are not selected. You have to assign all the cprId to a hidden field and then compare to selected checkboxes to extract which are not selected.
In here, Id should be in name field.
<TD><INPUT type="checkbox" name="10611583" value="cprld">TD>
<TD><INPUT type="checkbox" name="10611584" value="cprld"></TD>
<TD><INPUT type="checkbox" name="10611585" value="cprld"></TD>
<INPUT type="hidden" name="allcprId" value="10611583,10611584,1 0611585"
<%
// Get all selected checkboxes.
java.util.Set selSet = new java.util.TreeSet();
String id;
for (java.util.Enumeration e = request.getParameterNames( ); e.hasMoreElements() )
{
id = (String) e.nextElement();
if (request.getParameter(id). equalsIgno reCase("cp rld"))
{
selSet.add(id);
}
}
// Get all checkboxes id.
java.util.Set allSet = new java.util.TreeSet();
java.util.StringTokenizer st = new java.util.StringTokenizer( request.ge tParameter ("allcprId "), ",");
while (st.hasMoreTokens())
{
allSet.add(st.nextToken()) ;
}
// Get unselected checkboxes.
java.util.Set unselSet = new java.util.TreeSet(allSet);
unselSet.removeAll(selSet) ;
%>
In here, Id should be in name field.
<TD><INPUT type="checkbox" name="10611583" value="cprld">TD>
<TD><INPUT type="checkbox" name="10611584" value="cprld"></TD>
<TD><INPUT type="checkbox" name="10611585" value="cprld"></TD>
<INPUT type="hidden" name="allcprId" value="10611583,10611584,1
<%
// Get all selected checkboxes.
java.util.Set selSet = new java.util.TreeSet();
String id;
for (java.util.Enumeration e = request.getParameterNames(
{
id = (String) e.nextElement();
if (request.getParameter(id).
{
selSet.add(id);
}
}
// Get all checkboxes id.
java.util.Set allSet = new java.util.TreeSet();
java.util.StringTokenizer st = new java.util.StringTokenizer(
while (st.hasMoreTokens())
{
allSet.add(st.nextToken())
}
// Get unselected checkboxes.
java.util.Set unselSet = new java.util.TreeSet(allSet);
unselSet.removeAll(selSet)
%>
Consider using a vector instead of an array since the vector will grow automatically if it needs to where an array won't.
Just my opinion.
Just my opinion.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
"Accept Pellep's comment as answer."
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Kuldeepchaturvedi
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
"Accept Pellep's comment as answer."
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Kuldeepchaturvedi
EE Cleanup Volunteer