0
I need to identify checkbox fields that are unchecked via PHP
0
I need to identify checkbox fields that are unchecked via PHP
2
simple, assemble a ternary:
$checboxName = isset($_POST['name'])?true:false;
if it exists then it was set, if not.
In case you have many checkboxs
and do not wish to check them all via ternary, you must make a event
in javascript to set in value
in the input
before it is sent.
<?php
echo '<pre>';
var_dump($_POST);
echo '</pre>';
?>
<form method="post" id="form">
<input type="checkbox" name="name">
<input type="submit" value="send">
</form>
<script type="text/javascript">
document.getElementById('form').addEventListener('submit', function(){ // CRIA EM EVENTO QUE É DISPARADO QUANDO O ELEMENTO DE ID 'form' FOR 'submetido/enviado'.
var inputs = this.getElementsByTagName('input'); // PEGA TODOS OS INPUTS PRESENTES NESSE ELEMENTO
for(var i in inputs){ // ITERA OS INPUTS
var input = inputs[i];
if(input.type == 'checkbox'){ // CASO SEJA UM 'checkbox'
input.value = input.checked; // SETA 'value' COM TRUE/FALSE DE ACORDO COM O CHECKED
}
input.checked = true; // SETA COMO CHECKED PARA QUE ELE SEJA ENVIADO, O VALOR VALIDO É O QUE ESTA NO 'value' DO ELEMENTO
}
});
</script>
checked
are marked, even if they are not true, to solve this you can use ajax.$_POST
as string. Then even sent true/false, this sera string. To solve this problem you can use this function.function is_true($var){
if(is_string($var)){
if(preg_match('~^(false|f)$~', $var) != null){
return false;
}
}
return !!$var;
}
The case is that I have several checkbox, I did not want to rewrite at all, I wonder if there is a way for him to tell me if the input was not marked
@Tales updated, be if now this ok.
Browser other questions tagged php checkbox
You are not signed in. Login or sign up in order to post.
If it did not come in the POST/GET it is because this unchecked. If you want more details, it is important to add an excerpt of the HTML code with the checkboxes in the question to make it easier for those who answer, and explain how they are generated (if it is manual, if it is another PHP that generates, etc)
– Bacco