Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
custom pop up when clicked delete button using js in php

So i was making a project and i wanted to make it so when admin clicked on the delete button a custom alert popups which have three buttons "Yes" and "No" so if admin clicks on the yes button the user get deleted and if he clicks on the no buttons the users doesn't get deleted and the list remains the same i can make it so a the js alert popups and get the job done but its to simple and i wanted to have a customized alert box so here is the code

<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>

this is the code,

p.s. i am using sweet alert 2 for cutom alerts.

and as mentioned above i tried this

<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>

and it pops up a simple alert which i don't want so is there any way to do it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are not performing any action on result.isConfirmed

Try this. First add a class to your <td> and a data-attr element and remove the onclick attribute, we will let JavaScript handle this.

<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>

Please make sure you are using Jquery

For reference Integrate Sweetalert 2 In PHP & MySQL Using Ajax

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda