0

I have this input:

<input name="giftwrap" type="checkbox"/>

However, when the form is submitted and the checkbox has not been checked, I get a null rather than a false. I want a false to be submitted.

I'm feeling if I give the input a default value of unchecked it'll return false rather than null as if I tick and then untick the input I get false.

So how do I set the input as checked, and how do I set the input as unchecked?

Starkers
  • 10,273
  • 21
  • 95
  • 158

2 Answers2

0

You can add a checked attribute:

<input name="giftwrap" type="checkbox" checked/>

Or maybe you want something like this:

<form>
  <input type='hidden' value='0' name='selfdestruct'>
  <input type='checkbox' value='1' name='selfdestruct'>
</form>

Details: Post the checkboxes that are unchecked

(Read the comments as well.)

Community
  • 1
  • 1
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
0

$(function() {
  $('input[name=giftwrap]').on('change', function() {
    $(this).next().text(this.value = this.checked);//<-- note its assignment
  }).trigger('change');//<-- trigger on page load
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="giftwrap" type="checkbox" /><span></span>