As a complement, another solution that aims to ensure that the form will always send us a value for checkbox
whether it is checked or not, it passes by applying a input
hidden in the form to give us a value by default:
<input type="hidden" name="newsconf" value="0" />
<input type="checkbox" name="newsconf" id="newsconf" value="1" />
In this way, in PHP we always have the entry in the matrix that corresponds to the method of submitting the form, below an example for method="POST"
:
$querNewsletter = $_POST["newsconf"]; // vai otber 0 se não marcou ou 1 se marcou
Notes: To work as described above there are two things to keep:
The hidden field must be before the field that the user uses, so that the user field subscribes the value of the hidden field if the user actually marks the checkbox
.
The hidden field must have the value in the name
attribute equal to what we use in checkbox
.