0

My form runs in session scope.

I have two checks boxes in my form and they are selected by default. But when I uncheck the checkboxes and submit the form, the checkboxes reverts back to the checked state.

What could be causing this? Any hint on fixing this will be greatly appreciated.

FYI: If I change my form scope from session to request, the checkboxes seem too operate fine.

<div class="checkbox_item">
    <label>
        <input id="member" name="member" type="checkbox" checked="checked" value="Y" ></input>       
        Member       
    </label>
</div>

<div class="checkbox_item">
    <label>
        <input id="candidate" name="candidate" type="checkbox" checked="checked" value="Y"  ></input>       
        Candidate       
    </label>
</div>

I checked the value of the checkboxes like below before the form is submitted to the servlet and it always shows "Y" for both.

alert("member: " + $("#member").val() + " candidate: " + $("#candidate").val());

EDIT:

I am using Struts 1.

Method that renders the form:

public ActionForward renderForm(ActionMapping actionMapping,
        ActionForm form, HttpServletRequest req,
        HttpServletResponse res) throws Exception {
    MyForm myForm = (MyForm) form;

    myForm.set("member", "Y");
    myForm.set("candidate", "Y");

    return actionMapping.findForward("success");
}

Method that is called when the form is submitted:

public ActionForward search(
        ActionMapping actionMapping, ActionForm form,
        HttpServletRequest req,
        HttpServletResponse res) throws Exception {

    MyForm myForm = (MyForm) form;
    List<MyObject> list = service.search(myForm);
    myForm.setMyList(list);
    return actionMapping.findForward("success");
}

My jsp form:

<form name="myForm" action="myLink.do" method="post" onsubmit="return clickSearchButton()">    
    <label>
        <div class="checkbox_item">             
            <label>
                <html:checkbox name="myForm" styleId="member" property="member" value="Y"/>
                Member
            </label>
        </div>
        <div class="checkbox_item">
            <label>
                <html:checkbox name="myForm" styleId="candidate" property="candidate" value="Y"/>
                Candidate
            </label>
        </div>
    </label>
    <input type="submit" title="Search" />

</form>
Susie
  • 5,038
  • 10
  • 53
  • 74
  • Are you sure that your backend is not setting it to `checked` when the page loads? I see ...`checked="checked"` in your html – Vikram Feb 24 '14 at 18:29
  • 1
    Wouldn't $("#member").val() always show checked in this state. I think you need to check post data to see if the field was included. Because .val() will always return the value. If checkbox is unchecked then the value wont be included in POST data. – Sterling Duchess Feb 24 '14 at 18:31
  • @Vikram Initially when I go into that page I set them to checked from backend. But once on that page, I don't set it to anything. As can be seen from my "alert" statement, the value seems to be always "Y" even before it reaches the backend. – Susie Feb 24 '14 at 18:32
  • 1
    value `Y` has nothing to do with the checkboxes being checked..you have to check for `checked="checked"`. check for this: `alert("member: " + $("#member").attr('checked') + " candidate: " + $("#candidate").attr('checked'));` – Vikram Feb 24 '14 at 18:33
  • @kellax if the checkbox is unchecked, isn't it supposed to be set to empty string? But it always shows "Y" – Susie Feb 24 '14 at 18:34
  • @Susie Yes when POSTed. The problem is the .val() method from jQuery will always return the preset value in your case "Y". You need to actually post the data and check if the value is included your syntax is correct as far as HTML goes but your alert wont help you. – Sterling Duchess Feb 24 '14 at 18:35
  • look here: http://dev.w3.org/html5/markup/input.checkbox.html – Vikram Feb 24 '14 at 18:37
  • @Vikram That does not really help her her syntax is correct. If she unchecks the checkbox it wont appear in POST data. However what she does is she uses .val() method and this method will always return the value of the checkbox regardless if it's checked or not. If you unckeck the checkbox and then check your POST data you will see that checkbox was not included. – Sterling Duchess Feb 24 '14 at 18:39
  • Also you can't check POST data with JS you will have to check it in the PHP (or any other script) that receives the form data. – Sterling Duchess Feb 24 '14 at 18:41
  • @kellax I use java. I checked POSTed values of the checkboxes and they're always set to "Y" – Susie Feb 24 '14 at 18:44
  • @Vikram When I use your alert statement above, it shows true and false correctly for the checked and unchecked checkboxes. But why is this value not reflected in my variables in the backend? – Susie Feb 24 '14 at 18:50
  • @Susie Can you post the code you use to render and receive the form. – Sterling Duchess Feb 24 '14 at 18:51
  • @kellax I have posted my code in my question. Thank you – Susie Feb 24 '14 at 19:06

1 Answers1

1

Does this work for you? When a form is submitted, only "on" checkbox controls can become successful. On your backend you just need to evaluate presence of checkbox http://www.w3.org/TR/html401/interact/forms.html

if(null != myForm.get("member") && myForm.get("member")){// check if checkbox was checked/unchecked
   myForm.set("member","Y"); // use your business logic to set/unset this field accordingly
}
Vikram
  • 4,162
  • 8
  • 43
  • 65
  • This is what it seems I have to do. I have to manually set the values of "member" and "candidate" like you suggested. But why doesn't it happen automatically? I do have "member" and "candidate" variables in my form class. – Susie Feb 24 '14 at 19:43
  • Ohh my bad...my understanding was if you left a checkbox unchecked..ActionForm will not include that field in `POST` action. I came across this post: http://stackoverflow.com/a/16866448/405117 which may be useful. What it recommends is you must set the default value to `false`..I would rather not use the `value` attribute of a check-box to evaluate if it was checked/unchecked. – Vikram Feb 24 '14 at 19:52