<form> <input type="checkbox" key="1"/> <input type="hidden" name="checked[1]" value="false"> <input type="checkbox" key="2"/> <input type="hidden" name="checked[2]" value="false"> <input type="checkbox" key="3"/> <input type="hidden" name="checked[3]" value="false"> <input type="checkbox" key="4"/> <input type="hidden" name="checked[4]" value="false"> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function () { $('[key]').change(function () { var key = $(this).attr('key'); $($('[name="checked[' + key + ']"]')).val($(this).is(':checked') ? 'true' : 'false'); }); }); </script>
that's what I'm doing
I use two inputs: one flag without a name, so it will not be sent to php, the other is hidden, it will not be shown to the user, but this is what will be sent to php
then with jquery, when the user checks the jquery checkbox, change the value of the hidden input to true, and when you clear the checkbox, change the value to false
so the value will always be sent to php with true or false value as string
can you change the value you want to send to php by changing this .is(':checked')?'true':'false') something like .is(':checked')?1:0) to send 1 and 0 instead of true and false
another solution is rybo111 solution
<input type="hidden" name="check" value="false"> <input type="checkbox" name="check" value="true">
it will send two parameters, but if checked, it will override the first option
but it is not 100% reliable and it will send more data to the server
learn more about this in POSTing Form Fields with the same name attribute
therefore, if you want to use a simple solution without js, use only "html" if you want a 100% reliable solution to use "js"
source share