Link to home
Start Free TrialLog in
Avatar of anjugrao
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.
ASKER CERTIFIED SOLUTION
Avatar of pellep
pellep
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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,10611585"
<%
  // 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).equalsIgnoreCase("cprld"))
    {
      selSet.add(id);
    }
  }

  // Get all checkboxes id.
  java.util.Set allSet = new java.util.TreeSet();
  java.util.StringTokenizer st = new java.util.StringTokenizer(request.getParameter("allcprId"), ",");
  while (st.hasMoreTokens())
  {
    allSet.add(st.nextToken());
  }

  // Get unselected checkboxes.
  java.util.Set unselSet = new java.util.TreeSet(allSet);
  unselSet.removeAll(selSet);
%>
Avatar of HrdwareGuy
HrdwareGuy

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.
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