0
How can I make through toggle I update my database? I have toggle below:
For that, I’m doing it this way:
<div class="onoff">
<strong>Desabilitar notificações por e-mail:</strong>
<input type="checkbox" class="toggle" id="onoff">
<label for="onoff"></label>
<input type="hidden" name="Avisar" id="campo1" value="0">
</div>
......
<script type='text/javascript'>
//<![CDATA[
window.onload=function(){
var onoff = document.getElementById('onoff');
onoff.addEventListener('change', function() {
estado = this.checked ? 'S' : 'N';
var campo = document.getElementById("campo1").value = estado;
$.ajax({
url: 'notificacoes.php',
type: 'post',
data: {
estado: this.checked,
campo: campo
}
}).done(function(msg) {
});
});
}//]]>
///////////////////
</script>
And PHP:
....
$idUsuarios = $_SESSION["IdUsuarios"];
$status = $_POST["estado"];
....
mysqli_query($this->conexao,"UPDATE tabela SET Notificacoes = '".$status."' WHERE IdUsuario = '".$idUsuarios."';
Only it’s not updating. The problem is in Jquery, for the PHP is working properly.
Have you tried passing the date as a string? Ex.: date: "status="+this.checked+"&field="+field
– Henrique Pauli
You used a
var_dump
in php or an Alert onjavascript
to know if the request is being called when toggle is modified?– David Alves
tries to place an Alert in addeventlistener to see if it is being called.
– L.J
Hello guys. About passing as string, I did and did not pass the values. I gave an Alert as requested and the value (S or N) is coming correctly. We isolated PHP and ran a test directly on it and it’s working, so we ruled out that the problem is in PHP.
– user24136
The desired value arrives in php?
– David Alves
Yes... what I’m finding strange is that even the values being "S" and "N" when I echo the variable
$status
and a console.log(data), it returns me "TRUE" or "FALSE" and not "S" or "N". When I use a ternary in PHP, it returns only TRUE.– user24136
When I give console.log(status), it usually appears S or N.
– user24136
It’s like the value isn’t actually going to PHP.
– user24136