Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does this work for checkbox ?  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to see if a checkbox is checked, shall I check

1. in html or JSp, I define a checkbox and give it "MyCheckBox" as "Name".
2. if it is checked, request.getParameter("MyCheckBoxName").equalsIgnoreCase("on") will be true. Right ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben oliver:
If I want to see if a checkbox is checked, shall I check
2. if it is checked, request.getParameter("MyCheckBoxName").equalsIgnoreCase("on") will be true. Right ?


Not necessarily.
If your checkbox is created without a "value" attribute, the default value "on" will be sent up in the form request when the box is checked.

If you set a value, then the value will be sent up, if it is checked.

In either case, if the box is unchecked, the browser will send up nothing.
It will be as if the checkbox didn't exist in the form.
In this case, getParameter will return null.

Note: It will also return null if the name is missspelled.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you won't get true or false, but only the names of checkboxes that were checked when submitted. If the checkboxex have the same name, you'll have to call getParameterValues
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
I think that you won't get true or false, but only the names of checkboxes that were checked when submitted. If the checkboxex have the same name, you'll have to call getParameterValues


In his first post, the original poster was checking for "on" (the default value for a checked box) with the String.equalsIgnoreCase method; which returns true or false.

This brings up another issue that I should have mentioned in the first post.
The code:

will throw an NullPointerException if the checkbox is not checked.
In this case you will be trying to call a method of the String object when the string is null.

A more solid way of testing this would be to turn that line around.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it safe to assume that you can just check for null?



Seems simpler to me.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Isn't it safe to assume that you can just check for null?

Yep.
I was pointing out a more general issue that people run into when testing form parameters.
Checkboxes are somewhat unique in that null can be an expected value.
[ August 17, 2006: Message edited by: Ben Souther ]
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
[QB[/QB]



wouldn't that have thrown a null pointer if the MyCheckBoxName is null ie when the check box was not checked?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anselm Paulinus:

wouldn't that have thrown a null pointer if the MyCheckBoxName is null ie when the check box was not checked?


No, it will not. You can pass a null into an equals method without fear of an NPE.

This idiom of comparing a value that may or not be null to a string literal is quite commonly used.
[ August 17, 2006: Message edited by: Bear Bibeault ]
 
abhishek gowlikar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String _enabled=request.getParameter("enabled");
int ena=-1;
if("on".equals(_enabled))
{
ena=1;
}
else
ena=0;
<input type=checkbox" name="enabled">
<!-- Definatly it will Work ----->
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!