As a complement, another solution that aims to ensure that the form will always send us a value for the checkbox
whether it is marked or not, it involves applying a input
hidden in the form to provide us with a default value:
<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 input in the matrix that corresponds to the form submission method, below an example for method="POST"
:
$querNewsletter = $_POST["newsconf"]; // vai otber 0 se não marcou ou 1 se marcou
Notes: For it to work as described above there are two attentions to have:
The hidden field has to be before the field 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 has to have the value in the attribute name
same as the one we use in checkbox
.
The same happens if the
checkbox
is marked or not?– Maniero
yes, it’s the same
– Aron Brivio
It would be nice to put the
form
and a minimum of script PHP to see if the problem is not elsewhere.– Maniero
$_POST['newsconf']
will only exist if the checkbox is checked, useisset
to know whether or not he was marked.– rray