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?
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;
}