1 回答

TA贡献1851条经验 获得超4个赞
代码现在可以工作了,这就是它的最终结果。
管理模板:
if ($_POST['registro'] == 'eliminar') {
$id_borrar = $_POST['id'];
try {
$stmt = $conn->prepare('DELETE FROM admins WHERE id_admin = ?');
$stmt->bind_param('i', $id_borrar);
$stmt->execute();
if($stmt->affected_rows){
$respuesta = array(
'respuesta' => 'exito',
'id_eliminado' => $id_borrar
);
}else {
$respuesta = array(
'respuesta' => 'error'
);
}
} catch (Exception $e) {
$respuesta = array(
'respuesta' => $e->getMessage()
);
}
die(json_encode($respuesta));
}
管理阿贾克斯:
$('.borrar_registro').on('click', function(e) {
e.preventDefault();
let id = $(this).attr('data-id');
let tipo = $(this).attr('data-tipo');
Swal.fire({
title: '¿Estás Seguro?',
text: "Un usuario eliminado no se puede recuperar",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Si, Eliminar',
cancelButtonText: 'Cancelar'
}).then((result) => {
if (result.value) {
$.ajax({
type: 'post',
data: {
'id': id,
'registro': 'eliminar'
},
url: 'modelo-' + tipo + '.php',
success: function(data) {
console.log(data);
let resultado = JSON.parse(data);
if (resultado.respuesta === 'exito') {
Swal.fire(
'¡Eliminado!',
'El registro a sido eliminado',
'success'
)
jQuery('[data-id="' + resultado.id_eliminado + '"]').parents('tr').remove();
} else {
Swal.fire(
'¡Error!',
'No se pudo eliminar',
'error'
)}
}
})
} else if (result.dismiss === 'cancel') {
console.log(id);
Swal.fire(
'Cancelado',
'No se eliminó el registro',
'error'
)
}
});
});
- 1 回答
- 0 关注
- 75 浏览
添加回答
举报