3

my form has several checkboxes on it (around 15) and the issue im having is that the names of the check boxes only appear in the enumeration if they are checked but i want all of them to be returned so that when i print the data it will have the name of the checkbox and say "checked" or "unchecked". i had thought of one way that i could just manually set the flag to see what is present and what isnt, but that doesnt seem remotely efficient.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {

        try
        {
            FileWriter writer = new FileWriter("OrderFormData.csv");
            writer.append("FieldName");
            writer.append(',');
            writer.append("Value");
            writer.append('\n');
            @SuppressWarnings("unchecked")
            Enumeration <String> paramNames = request.getParameterNames();
            while(paramNames.hasMoreElements()) 
            {
                String paramName = (String)paramNames.nextElement();
                writer.append(paramName);
                writer.append(',');
                String[] paramValues = request.getParameterValues(paramName);
                if (paramValues.length == 1)
                {
                    String paramValue = paramValues[0];
                    if (paramValue.length() == 0)
                    {
                        writer.append("No Value");
                        writer.append('\n');
                    }
                    else
                    {
                        writer.append(paramValue);
                        writer.append('\n');
                    }
                }
                else
                {
                    for(int i = 0; i<paramValues.length; i++)
                    {
                        writer.append(paramValues[i]);
                        writer.append(',');
                    }
                    writer.append('\n');
                }


            }
            writer.flush();
            writer.close();
        }
        catch(IOException e)
        {
             e.printStackTrace();
        }

    }
}
kosa
  • 65,990
  • 13
  • 130
  • 167
StraightEdge
  • 65
  • 1
  • 3
  • 8

4 Answers4

6

You should think in a workaround because as @Alex told you, unchecked checkboxes are not part of the request.

This is just an idea, but for example:

<input type="hidden" name="checkboxNamesList" value="nameCheckbox1" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox2" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox3" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox4" />

Then, you can get checkboxNamesList from the request (it will be a String[]) so you will have all the checkboxes params names. If you get a parameter for one of the checkboxes name and value is null, it means that checkboxe was not checked.

Edited: clarification

Well, as unchecked checkboxes are not present in the request you don't know whose checkboxes in the JSP were not checked but you need to know it in order to write in your data file something like checkbox_name1=unchecked.

So, how to do that? First, you need to know what checkboxes (uncheck or not) were present in the request. For that, you can use the code below and get name of all checkboxes present in the JSP:

String[] checkboxNamesList= request.getParameterValues("checkboxNamesList");

Then, let look for unchecked checboxes:

for (int i = 0; i < checkboxNamesList.length; i++) {

    String myCheckBoxValue = request.getParameterValues(checkboxNamesList[i]);

    // if null, it means checkbox is not in request, so unchecked 
    if (myCheckBoxValue == null)
        writer.append(checkboxNamesList[i] + "=unchecked");

    // if is there, it means checkbox checked
    else
        writer.append(checkboxNamesList[i] + "=checked");

}
jddsantaella
  • 3,657
  • 1
  • 23
  • 39
  • the issue i have with that is i cant leave the hidden textfields in the data file, because this is going to imported into another program. the file is listed by the name of the field and the value...if it is true it will return both, but i only want it to return the actual checkbox when it is true, and not return the hidden one – StraightEdge Jul 30 '12 at 21:33
  • What do you mean by "data file"? Hidden fields are just part of the request. The goal here is to find out the names of those checkboxes that were not checked so you can do with them whatever you want. – jddsantaella Jul 30 '12 at 21:37
  • im exporting data from the form into a csv file, and it is formatted by: field name field value will it not return the hidden field? because if i understand correctly it will return the actual checkbox when it is true and it will also return the hidden textfield when it is false which isnt desired – StraightEdge Jul 30 '12 at 21:37
  • OK. So what is the problem? If you have a list of checkboxes (checked and unchecked), look for them into the request, if param is there, checkbox is checked, if not, unchecked. – jddsantaella Jul 30 '12 at 21:49
  • im sorry, im not trying to be rude or anything. i just honestly dont see how this works for solving my problem. im not doubting that it does work, im just trying to understand how, so that i can implement it, i genuinely just want to understand. Like how does that work...wont this method return: fieldname field value chkbox1 true chkbox1txt false chkbox2 false chkbox2txt false where chkbox1txt and chkbox2txt are the hidden text boxes. if it does i want to eliminate having the true and the false for the same "elememnt" (the checkbox and hidden field) – StraightEdge Jul 30 '12 at 21:52
  • You were not rude ;-) I have edited my answer. I hope it is clearer now. Let me know. By the way, I did not tested the code, so it may not work just copying pasting. – jddsantaella Jul 30 '12 at 22:08
  • that was much clearer, sorry it was the end of the day and was not thinking clearly, it looks so obvious now. thanks again! – StraightEdge Jul 31 '12 at 15:36
  • I could be missing something out, but from my experience I gathered that request.getParameterValues("checkboxNamesList"); only returns the CHECKED boxes – Chris Neve Oct 07 '14 at 14:21
1

You can use hidden fields together with checkboxes. Iterate through them and check the presence of checkbox selection.

<input type="checkbox" name="fruit" value="Apple" />
<input type="hidden" name="fruit_option" value="Apple" />
<input type="checkbox" name="fruit" value="Orange" />
<input type="hidden" name="fruit_option" value="Orange" />

And at the server side do something like this:

Enumeration<String> paramNames = req.getParameterNames();
while(paramNames.hasMoreElements()) {
    String paramName = paramNames.nextElement();

    if (!paramName.endsWith("_option")) continue; // Skip checkboxes. Only process hidden fields

    String [] options = req.getParameterValues(paramName); // All options
    String [] selections = req.getParameterValues(paramName.replace("_option", "")); // Real checkboxes

    List <String> selectionList = Arrays.asList(selections); // Easier to work with

    // Iterate through checkbox group options
    for (String o : options) {
        boolean contains = selectionList.contains(o);
        // Do something with it. Write yes/no to file for example...
    }
}
tuner
  • 326
  • 3
  • 9
  • ive seen this solution before, how exactly does that help determine which is checked and which isnt? – StraightEdge Jul 30 '12 at 21:26
  • Take care. If you use the same name ("fruit") for all checkboxes you will be working with a group of checkboxes where just one can be checked. – jddsantaella Jul 30 '12 at 21:33
  • That's true. I probably used something like this on a form with plenty of dynamic checkboxes and radiobuttons. The latter have to be in a group and the same code works for both. – tuner Jul 30 '12 at 21:59
0

unchecked boxes won't be part of the request: see http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.2

Alex Snaps
  • 1,230
  • 10
  • 10
-1

Create hidden fields as

<input type="checkbox" name="checkbox" checked/> <input type="hidden" name="checkbox" checked />

Now in your servlet as: String[] names = request.getParameterValues("checkbox");

    PrintWriter pw = new PrintWriter(new File("/Desktop/sticker.txt"));
    for(int i=0; i < names.length; i++) {
        if(i + 1 < names.length && names[i].equals(names[i+1])) {
            pw.write(names[i] + ",true\n");
            ++i;
        } else {
            pw.write(names[i]+",false\n");
        }

    }
    pw.close();
Victor
  • 761
  • 8
  • 7