2
I would like to have a checkbox button on the site to enable and disable some action, I am using ajax to make the change immediately, the action would not need an F5 to work. The problem is that I am passing the id of the div created by php by the name of the check and it does not seem to be working because php does not change the value of check to 1 in the database. I would like to change its value from 1 - 0
<?
php
session_start();
include_once("conexao.php");
$ativado = $_POST['ativado'];
$nome = $_POST['nome'];
$sql="UPDATE produto SET ID=$nome WHERE ativado='$ativado'";
$query = mysqli_query($conn, $sql);
echo $ativado;
?>
<div class='col-3'>
<label class='switch mt-3'>
<input class='input-check'type='checkbox' name='check".$row_produto['ID']."' value='0'>
<span class='check round'></span>
</label>
<button type='button' class='close deletar mt-2' id='".$row_produto['ID']." aria-label='Close'>
<i class='fas fa-trash-alt'></i>
</button>
</div>
$(document).ready(function () {
$('input[type="checkbox"]').click(function () {
var ativado = $(this).attr('value');
var nome = $(this).attr('name');
if ($(this).prop("checked") == true) {
$('input[name=' + nome + ']').val('0');
alert("Checkbox is checked. " + nome + "valor= " + ativado);
$.ajax({
type: 'POST',
url: '../php/ativo_produto.php',
data: {
ativado: ativado,
nome: name,
},
success: function (response) {
alert(response);
}
})
} else if ($(this).prop("checked") == false) {
$('input[name=' + nome + ']').val('1');
alert("Checkbox is checked. " + nome + "valor= " + ativado);
$.ajax({
type: 'POST',
url: '../php/ativo_produto.php',
data: {
ativado: ativado,
nome: name,
},
success: function (response) {
alert(response);
}
})
}
});
});
When checked the value of the checkbox is
0
, and when it’s not there the value is1
? Wouldn’t it be the other way around?– Sam
I changed it unintentionally. But anyway it doesn’t even get called or changed in the bank
– lincoln ferreira
What do you mean it’s not even called? It doesn’t give the Alert? It has several problems
– mari
it is not changed, because the value that is there is 0 and wanted to change to 1
– lincoln ferreira