Toggle update mysql database automatically

0

How can I do that through toggle I update my database? I have the toggle below:

Forthis,I'mdoingitthisway:

<divclass="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 you are not updating. The problem is in Jquery , because PHP is working correctly.

    
asked by anonymous 01.06.2018 / 18:27

1 answer

1

I do not quite understand the difference between status and field , but I believe which field will pass which field you want to enable or disable notifications and status is the yes and no.

If this is the case, I suggest changing it:

var campo = document.getElementById("campo1").value = estado;

To:

var campo = document.getElementById("campo1").value;

Because field1 was changing its value when it changed status. (If this should occur ignore this part)

Then you set the status to S or N but pulled this.checked

estado: this.checked,

When should it be:

estado: estado

I hope this helps you!

    
01.06.2018 / 21:59