3

i have checkbox on the form

<input class="addToFavorite" type="checkbox" name="addToFavorite"> Add to favorite

now when form posting i check if this checkbox checked with this code. But it return true every time. how i can check if checkbox has been really checked?

boolean wantAddToFavorites = false;
            if (isPayAction) {
                wantAddToFavorites = request.getParameter("addToFavorite").equals("on");
            } 

FireBug result

enter image description here

as you see It always send its value

Community
  • 1
  • 1
AEMLoviji
  • 3,217
  • 9
  • 37
  • 61

2 Answers2

4

If you want to check on server-side if a checkbox has been checked or not, you should do the following:

1. Add a value to your checkbox

<input class="addToFavorite" type="checkbox" name="addToFavorite" value="addToFavourite"> Add to favorite</input> 

2. Check this checkbox value on server-side

if(request.getParameter("addToFavorite") == null){
    //checkbox not checked
}else{
    //checkbox checked
}
Daniel
  • 1,861
  • 1
  • 15
  • 23
  • no it does not work. i have added value attribute to the checkbox. but condition request.getParameter("addToFavorite") == null everytime returns true – AEMLoviji Aug 20 '14 at 10:07
  • Check again your checkbox name and the value from `getParameter(..)` call. They must match, else it'll not work. Also make sure that the name you've used for your checkbox is unique. If you've grouped multiple checkboxes under the same name , then you must use `request.getParameterValues("addToFavorite")` – Daniel Aug 20 '14 at 10:34
  • please see my post. i have added FireBug screenshot – AEMLoviji Aug 20 '14 at 11:52
  • Please update your post with more from your server-side code. Maybe that's where the problem is. I'm quite sure that this must work. – Daniel Aug 20 '14 at 11:56
  • yes you are right. it must work. But does not work now on me. I have added server side code before – AEMLoviji Aug 20 '14 at 12:09
  • I see your server-side code as being : `wantAddToFavorites = request.getParameter("addToFavorite").equals("on");` . Did you try the following : `wantAddToFavorites = request.getParameter("addToFavorite") != null;` ? – Daniel Aug 20 '14 at 12:14
  • yeees of course. as you see on FireBug it posts it's value. And your condition to check is not null will be false – AEMLoviji Aug 20 '14 at 12:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59652/discussion-between-daniel-and-aemloviji). – Daniel Aug 20 '14 at 12:25
3

In a checkbox, the value attribute holds the string that will be sent if the box is checked. By default it sends the string "on".

What determines whether it is checked or not is the checked attribute.

Example:

    <input type="checkbox" name="check1" checked />               Sends "on"  
    <input type="checkbox" name="check2" />                       Sends null
    <input type="checkbox" name="check3" value="hello" checked /> Sends "hello"  
    <input type="checkbox" name="check3" value="hello" />         Sends null.