Entonces, estaba haciendo un proyecto y quería que cuando el administrador hiciera clic en el botón Eliminar, apareciera una alerta personalizada que tiene tres botones "Sí" y "No", de modo que si el administrador hace clic en el botón Sí, el usuario se elimina y si él hace clic en los botones no, los usuarios no se eliminan y la lista sigue siendo la misma. Puedo hacerlo para que aparezca la alerta js y hacer el trabajo, pero es demasiado simple y quería tener un cuadro de alerta personalizado, así que aquí está el código
<tbody> <tr> <td> <?php echo $value['0']; ?> </td> <td> <?php echo $value['1']; ?> </td> <td> <?php echo $value['2']; ?> </td> <td> <?php echo $value['3']; ?> </td> <td> <?php echo $value['4']; ?> </td> <td><img src="./assets/img/<?php echo $value['5'];?>" width="150px"</td> <td> <?php echo $value['6']; ?> </td> <td> <?php echo $value['7']; ?> </td> <td><a href="./updateUser.php" class="btn btn-success">Edit</a></td> <td><a href="./users.php?id=<?php echo $value[0] ?>" onclick="confirmDelete()" class="btn btn-danger">Delete </a></td> <script> function confirmDelete() { Swal.fire({ title: 'Are you sure you want to Delete this entry?', showDenyButton: true, showCancelButton: true, confirmButtonText: 'Yes', denyButtonText: `No`, }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { Swal.fire('Deleted!', '', 'success') } else if (result.isDenied) { Swal.fire('Deletion Was Cancelled', '', 'info') } }) } </script> </tr> <?php if (isset($_GET["id"])) { $id = $_GET["id"]; $query_remove = "DELETE FROM `users` WHERE `id` = $id"; mysqli_query($conn, $query_remove); } ?> </tbody>este es el código,
PD: estoy usando Sweet Alert 2 para alertas personalizadas.
y como se mencionó anteriormente probé esto
<td><a href="./users.php?id=<?php echo $value[0] ?>" onclick="return confirm('are you sure you want to delete??')" class="btn btn-danger">Delete</a></td>y aparece una alerta simple que no quiero, ¿hay alguna forma de hacerlo?
No está realizando ninguna acción en result.isConfirmed
Prueba esto. Primero agregue una clase a su <td> y un elemento data-attr y elimine el atributo onclick , dejaremos que JavaScript maneje esto.
<td><a href="javascript:void(0)" data-id="<?php echo $value[0] ?>" class="btn btn-danger btn-delete-user">Delete </a></td> <script> $(document).delegate(".btn-delete-user", "click", function() { Swal.fire({ title: 'Are you sure you want to Delete this entry?', showDenyButton: true, showCancelButton: true, confirmButtonText: 'Yes', denyButtonText: `No`, }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { var userId = $(this).attr('data-id'); // Ajax config $.ajax({ type: "GET", //we are using GET method to get data from server side url: './users.php', // get the route value data: {id:userId}, //set data beforeSend: function () {//We add this before send to disable the button once we submit it so that we prevent the multiple click }, success: function (response) {//once the request successfully process to the server side it will return result here Swal.fire('Success.', response, 'success') } }); } else if (result.isDenied) { Swal.fire('Changes are not saved', '', 'info') } }); }); } </script> Por favor, asegúrese de estar usando Jquery
Para referencia Integrar Sweetalert 2 en PHP y MySQL usando Ajax