We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

checkbox selection....

Jagadeesh M
Jagadeesh M asked
on
Medium Priority
374 Views
Last Modified: 2010-03-31
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="kiran" value="10611583">TD>
<TD><INPUT type="checkbox" name="kumar" value="10611584"></TD>
<TD><INPUT type="checkbox" name="jags" value="10611585"></TD>



i want to capture (in a servlet) all the values of the checkboxes that are checked and store them in a variable ...(in an array) Any help would be greatly appreciated.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Give them all the same name then

String[] vals = request.getParameterValues("theName");

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

Commented:
and if you dont want to give the same name to all

Enumeration<String> e = request.getParameterNames();
ArrayList<String> values = new ArrayList<String>();
while(e.hasMoreElements()) {
      values.add(e.nextElement());
}
Top Expert 2006

Commented:
just correct it
values.add(request.getParameter(e.nextElement()));
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
And then if you want them in an array take the code posted by hoomanv and do:

String[] array = values.toArray(new String[0]);
Top Expert 2006

Commented:
yes thats true but i asked myself what is the usefulness of getting just name's values, whitout the names, they are just pure numbers that are passed to the servlet, so why did he give them names at the first place. however ... its up to him
Jagadeesh MBig Data and Splunk Architect

Author

Commented:
hoomanv and CEHJ,

I believe your codes would get me only the values of the checked checkboxes....please confirm.
thankz.
CERTIFIED EXPERT
Top Expert 2016

Commented:
request.getParameterNames() returns the names of ALL form elements (i.e. including any text areas, buttons etc.), which has been used in the other code to get ALL the form elements
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you should be prexixing your parameter names so you can identify which are checkboxes

<TD><INPUT type="checkbox" name="names_kiran" value="10611583">TD>
<TD><INPUT type="checkbox" name="names_kumar" value="10611584"></TD>
<TD><INPUT type="checkbox" name="names_jags" value="10611585"></TD>

Enumeration<String> e = request.getParameterNames();
ArrayList<String> values = new ArrayList<String>();
while(e.hasMoreElements()) {
    String name = e.nextElement();
    if (name.startsWith("name_")) {
       values.add(e.nextElement());
    }
}
String[] names = values.toArray(new String[0]);
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.