Thanks for all the help. I'm now able to get value from checkbox type
input.
Thanks.
- Thomas -
Hans Bergsten wrote:
>
> Thomas To wrote:
> >
> > Thanks for all the help.
> >
> > My situation is slightly different. Following is part of the html code:
> >
> > <td><input type="checkbox" name="OtherInterest" value="Traveling">
> > Traveling</td>
> > <td><input type="checkbox" name="OtherInterest" value="Fishing">
> > Fishing</td>
> >
> > Name "OtherInterest" is not known to the servlet. I can't hard coded it
> > in the servlet. That's why it causes problems when user doesn't check
> > either Fishing or Traveling because the parameter name "OtherInterest"
> > will not be return by getParameterNames() at all.
> >
> > The reason I need to put "Traveling" and "Fishing" as the values rather
> > then "true" is because I need the actual string. So if user click on
> > "Traveling", the string "Traveling" will be return. If both were
> > checked, the return string will be "Traveling,Fishing" by calling
> > req.getParameter(); I need the actual strings "Traveling" and "Fishing"
> > to update the database.
>
> I'm not sure if this will help you, but it might.
>
> You can add a hidden field with the same name as the checkbox fields and
> an empty string as its value. This way you will always get a parameter
> with this name, even if none of the checkboxes is checked.
>
> In you servlets you could do something like this:
>
> Hashtable parsedParams = new Hashtable();
> String[] names = req.getParameterNames();
> for (int i = 0; i < names.length; i++) {
> String param = names[i];
> String[] values = req.getParameterValues(param);
> Vector parsedValues = new Vector();
> for (int j = 0; j < values.length; j++) {
> String value = values[i];
> // Get rid of empty strings
> if (value.length() != 0) {
> parsedValues.addElement(value);
> }
> }
> parsedParams.put(param, parsedValues);
> }
>
> Enumeration validParams = parsedParams.keys();
> while (validParams.hasMoreElements) {
> Vector validValues = (Vector) validParams.nextElement();
> if (validValues.size() == 0) {
> // Process empty text field or empty checkbox value
> }
> if (validValues.size() == 1) {
> // Process filled out text field or single-value checkbox value
> }
> if (validValues.size() > 1) {
> // Process multi-value checkbox value
> }
> }
>
> If you need a way to distinguish between empty/single-value text fields
> and empty/single-valued checkboxes you can use some naming convention
> for the field names.
>
> I hope this helps you solve the problem.
>
> --
> Hans Bergsten [EMAIL PROTECTED]
> Gefion Software http://www.gefionsoftware.com
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html