1

I have a form/input checkbox like so:

<form id="the_form">
    <input name="my_checkbox" type="checkbox" value='true' />
</form>

When I run a $("#the_form").serialize(); my_checkbox only comes up as true when it is checked. If it is not checked, there is no data that comes with my_checkbox.

Is there a way I can make it so if the data is not checked, it'll at least give a null value to my_checkbox or something?

3
  • my_checkbox only comes up as true when it is checked. If it is not checked, there is no data that comes with my_checkbox. But of course! They work as they are supposed to! What do you mean by Is there a way I can make it so if the data is not checked, it'll at least give a null value to my_checkbox or something? As far as I know in the programming world most things other than TRUE are considered FALSE
    – ilgaar
    Commented Feb 24, 2015 at 18:41
  • my_checkbox= is a lot different than sending no post data. Right now it'll either send my_checkbox='true' or nothing. I'd like it to say my_checkbox='false' if it isn't checked
    – bryan
    Commented Feb 24, 2015 at 18:44
  • How about checking if a certain checkbox data was sent or not?If it was sent then it is most definitely true otherwise you can assign a false value to my_checkbox if you really have to.
    – ilgaar
    Commented Feb 24, 2015 at 18:49

2 Answers 2

4

This is normal behavior for HTTP form posts from web browsers: checkbox values are not presented when not checked, since you can infer their value from their absence.

Frankly, this is can be solved with plain engineering: the code that presents the form knows if the checkbox should be present, so the code that receives the post should also know that the checkbox should be present and can implicitly determine the correct, non-checked, value of the checkbox.

If you will give me an indication of your back end technologies I may be able to provide some ideas for how you might engineer the presence of a value for non-checked checkboxes in your form data collection.

If you truly must have the value posted, which I consider less than ideal, then you can add a hidden input that mirrors the value of your checkbox and which is updated by JavaScript:

<form id="the_form">
    <input id="my_checkbox_ui" name="my_checkbox_ui" type="checkbox" value='true' />
    <input id="my_checkbox" name="my_checkbox" type="hidden" value='true' />
</form>
<script>
   $(document).ready(function () {
      $('#my_checkbox_ui').change(function () {
         $('#my_checkbox').val($(this).val());
      });
   });
</script>

Keep in mind that doing this when not absolutely needed creates extra complexity and yields a poorer system:

  • The form post will be larger, using more bandwidth
  • The simple correspondence between visible elements and underlying posted elements is broken, a possible source of confusion and mistakes for future developers working with this code.
  • The need for JavaScript creates additional surface area of possible breakage--there are more moving parts that can break and this causes fragility.
  • Related to the two previous points, if someone copies the form and tries to use it in other code, it won't work correctly, even though the UI looks like it is working.

Another way that you could possibly solve this is to semi-unobtrusively injection the the checkbox-shadowing elements with javascript:

<form id="the_form">
    <input id="my_checkbox_ui" name="my_checkbox_ui" type="checkbox" value='true' />
    <input id="my_checkbox" name="my_checkbox" type="hidden" value='true' />
</form>
<script>
   function getShadowFn(id, shadowId) {
      return function() {
         $('#' + shadowId).val($('#' + id).val());
      };
   }

   function convertCheckboxesToAlwaysPostingCheckboxes() {
      $('input[type="checkbox"]').each(function(id, el) {
         var $el = $(el);
         var name = $el.prop('name');
         var id = $el.prop('id');
         var shadowId = id + '_shadow'
         $el.prop('name', name + '_ui');
         $('<input type="hidden" id="' + shadowId + '" name="' + name + '">')
            .insertAfter($el);
         $el.change(getShadowFn(id, shadowId));
      });
   }

   $(document).ready(convertCheckboxesToAlwaysPostingCheckboxes);
</script>

This is still added complexity, but it is less fragile because it is no longer specific to any checkbox, and all the checkboxes will now work correctly with just this one bit of code (though I didn't test it at all, so please take it as inspiration rather than prescription). So you can get this code working right, and then use it over and over.

4
  • Thank you. I appreciate the rundown. Unlike my example, in my real world production I have many checkboxes and I feel like this wouldn't be the appropriate way to solve this.
    – bryan
    Commented Feb 24, 2015 at 18:35
  • Please see my update which suggests a different way of accomplishing the same task that may be more palatable to you. Please consider that I did tell you an appropriate way to solve this: through engineering on the back end.
    – ErikE
    Commented Feb 24, 2015 at 18:51
  • I appreciate the help @ErikE, but I just wanted the information to be relayed through the post as a true or false. I've found out the answer on my own by doing a check to see if the input was checked or not.
    – bryan
    Commented Feb 24, 2015 at 18:56
  • Frankly, your way is better. :)
    – ErikE
    Commented Feb 24, 2015 at 18:56
3

I submit my checkbox through javascript, and since I parse a lot of information in the backend, and my script looks for these variables, it didn't seem like an appropriate solution to do this after the form was submitted.

Thanks to everyone that gave me guidance, but I figured out a way to solve this without too much heavy lifting.

function submitForm()
{
    //serialize all the inputs in the form
    var data_serialize = $("#the_form").serialize();

    //check to see if any checkbox's are not checked, and if so, make them false
    $("#the_form input:checkbox:not(:checked)").each(function(e){ 
          data_serialize += "&"+this.name+'=false';
    });

    //submit the form
    var url = "/submit";
    $.ajax({ type: "POST", url: url, data: data_serialize,
        success: function(data) {  },
        error: function(data) {  }
    });
    return false;
}
2
  • 1
    You're absolutely right, I focused on the form/post part of the equation and not the serialization. In fact, I was about to add to my answer a solution somewhat like this--sorry I didn't think of it sooner! +1.
    – ErikE
    Commented Feb 24, 2015 at 18:55
  • +1 for this solution. It lets to customize the "unchecked" value with false/0/no/whatever depending on needs. Commented Dec 29, 2015 at 12:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.