After several attempts and searches I turn to you for the following problem. In a table I have a field in the DB that gives me the possibility to Activate or Not a record (for example, so that it can not be seen). I have a form to update high, but I need as that direct access and not bring all the data of the DB to update that single field. Since they can not have all the records the same ID is that I tried to do it with a function and passing the parameters to it. Use Bootstrap for the styles, hence if it is active, the "S" will be green or the "N" will be green. So far what I got is that by clicking on the respective button I change the "S" in the DB the "N" and vice versa, but I do not see the change of the letter or format without updating the page.
HTML
<table class="table table-condensed table-striped">
<tr>
<th class="text-center" width="5px">Activo (S/N)</th>
<th class="text-center" width="120px">Acción</th>
</tr>
<tr>
<td class="text-center"><button onclick="modificar(18,'S')" class="btn btn-success btn-lg">S</button></td>
<td class="text-center">
<a class="btn btn-warning btn-lg glyphicon glyphicon-pencil" href="formAlertas.php?id=18" role="button" title="Editar"></a>
<a class="btn btn-danger btn-lg glyphicon glyphicon-trash" href="borraAlertas.php?id=18" role="button" title="Eliminar" onclick="return confirm('¿Estas seguro?');"></a>
</td>
</tr>
<tr>
<td class="text-center"><button onclick="modificar(19,'N')" class="btn btn-success btn-lg">N</button></td>
<td class="text-center">
<a class="btn btn-warning btn-lg glyphicon glyphicon-pencil" href="formAlertas.php?id=19" role="button" title="Editar"></a>
<a class="btn btn-danger btn-lg glyphicon glyphicon-trash" href="borraAlertas.php?id=19" role="button" title="Eliminar" onclick="return confirm('¿Estas seguro?');"></a>
</td>
</tr>
</table>
PHP (active-alert.php)
<?php
$id = $_POST['id'];
if( $_POST['activo'] === 'N' ) {
$activo = 'S';
} else {
$activo = 'N';
}
$sql = "UPDATE 'tbl_alertas' SET 'activo'='$activo' WHERE 'id'='$id'";
if (mysqli_query( $conn, $sql )) {
echo ("exitoso");
} else {
echo ("invalido");
}
mysqli_close($conn);
?>
JQUERY
</table>
<script>
function modificar(idz,acti){
var elem = $(this);
var id = idz;
var activo = acti;
$.ajax({
type: "POST",
url: "activa-alerta.php",
data: {id: id, activo:activo},
success : function(text,elem){
if (text == "exitoso"){
alert(elem);
esExitoso(elem);
}
else {
esError();
}
}
});
function esExitoso(elem){
if ( activo === 'S' ) {
alert(activo);
elem.removeClass( "btn-danger" );
elem.html('S');
elem.addClass( "btn-success" );
} else {
alert(activo);
elem.removeClass( " btn-success" );
elem.html('N');
elem.addClass( "btn-danger" );
}
};
function esError(){
alert('error');
};
}
</script>
</body>
</html>
I think I do not forget anything. Thanks