Question:
When I submit the form, the input
does not return the checkbox
value and says that the $_POST['newsconf']
does not exist.
HTML
<input type="checkbox" id="newsconf" name="newsconf" value="1"/>
<label for="newsconf">Assinar newsletter</label>
What am I doing wrong so I am not getting the value of the checkbox
?
Answer:
I did a test and it worked as it should. You made a statement in the comment that I don't believe is actually happening:
<form action="index.php" method="post">
<input type="checkbox" id="newsconf" name="newsconf" value="1"/>
<input type="submit" name="submit" value="Ok"/>
</form>
PHP
<?php
if(isset($_POST['newsconf']) && $_POST['newsconf'] == '1') {
echo "checkado";
} else {
echo "não checkado";
}
?>
I put it on GitHub for future reference .
When the field is not marked it is not sent by the form, so you need to check if it exists to see if it is marked.
If not, there is no way to know what the problem is with what was posed in the question.