Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

get values of checkbox group

I have a form that contains radio groups and checkboxe groups as well, each checkbox group / radio group has a unique name.

In my Servlet I tried to get all checked values for those radio groups and checkbox groups.

PrintWriter out = response.getWriter();
Enumeration<String> requestParameters = request.getParameterNames();

while (requestParameters.hasMoreElements()) {
    String paramName = (String) requestParameters.nextElement();
    out.println("Request Paramter Name: " + paramName
              + ", Value - " + request.getParameter(paramName));
 }

When I select multiple checkboxs in a checkbox group I only get one value.

For example, I have this checkbox group :

<ul class="data-list-2">        
    <li>
       <div class="icheckbox_square-aero">
          <input style="position: absolute; opacity: 0;" name="question_1" class="required" value="Mise en page d’un texte" type="checkbox">
       </div>
       <label>Mise en page d’un texte</label>
    </li>

    <li>
       <div class="icheckbox_square-aero">
          <input style="position: absolute; opacity: 0;" name="question_1" class="required" value="Compilation d’un texte" type="checkbox">
       </div>
       <label>Compilation d’un texte</label>
    </li>

    <li>
       <div class="icheckbox_square-aero">
          <input style="position: absolute; opacity: 0;" name="question_1" class="required" value="Présentation d’un texte sous forme de diaporama" type="checkbox">
       </div>
       <label>Présentation d’un texte sous forme de diaporama</label>
    </li>

    <li>
       <div class="icheckbox_square-aero">
          <input style="position: absolute; opacity: 0;" name="question_1" class="required" value="Edition d’un texte" type="checkbox">
       </div>
       <label>Edition d’un texte</label>
    </li>
</ul>

And I checked the last three checkboxs, I only get one value as the following :

Request Paramter Name: question_1, Value - Compilation d'un texte

I googled about this, and I found that I must add a [] after the name of each checkbox group, but when I tried that, this is what I got :

Request Paramter Name: question_1[], Value - Compilation d'un texte

How can I solve this ?

Upvotes: 1

Views: 340

Answers (1)

Sudarshan_SMD
Sudarshan_SMD

Reputation: 2587

Checking multiple checkbox in form will give you values in String array form. You will have to use request.getParameterValues() inorder to get all values that you have selected in an String array.

Your sample could be like:

String[] questions = request.getParameterValues("question_1");
for(int i=0;i<questions.length;i++){
out.println(questions[i]);
}

Upvotes: 1

Related Questions