How to confirm before deleting a record with PHP and AlertifyJS?

0

I have a question with the confirmation before deleting a record from my Database, for this I am using PHP and the AlertifyJS library, in my HTML structure I have a link through which I send by GET method the id of the person to delete, in this same link I have a class called delete which I use in JQuery to trigger the function that contains the alertify.confirm , it is worth mentioning that if you delete the person but the problem is that he sends me the confirmation but he does not let me decide if I want to eliminate the user or not.

<a href="becarios.php?becarioid=<?php echo $registro['id_becario'];?>" class="enlace-table elimina" data-toggle="tooltip" data-placement="bottom" title="Eliminar Becario" alt="Eliminar Becario">
    <i class="fas fa-times-circle"></i>
</a>

JQuery code

$(document).ready(function() 
{

   $('.elimina').on('click', function() {
       validaBaja();
   });
});
 function validaBaja() {
       alertify.confirm("Deseas eliminar al usuario",
            function(){
               alertify.success('Eliminado');
       },
       function(){
           alertify.error('Cancelado');
       }
      );
}

PHP Code

$delBecario = verificaId($_GET['becarioid']);

if (!empty($delBecario)) 
{
   $sql_del_becario = "DELETE FROM becarios WHERE id_becario='$delBecario'";
   $elimina = eliminaRegistro($sql_del_becario, $conecta);
   if ($elimina !== false) 
   {
      header('Location:becarios.php');
   }
}

Greetings.

    
asked by Ivan18 19.10.2018 в 21:58
source

2 answers

0

First, you should not delete a record using a GET , you would use a POST (or a DELETE if it is an Api REST). Then, I would use a function in JS to delete the record using Ajax.

Create a button that has a data attribute with the id to be deleted.

<button type="button" class="elimina" data-becarioId="<?php echo $registro['id_becario']; ?>">Eliminar</button>

Then the JS, in the click event, gets the id that is associated with the button by means of the data attribute. You pass it by parameter to the function it eliminates.

$(document).ready(function() {

  $('.elimina').on('click', function() {
    var id = $(this).data('becarioId');
    eliminar(id);
  });

  var eliminar = function(id) {
    alertify.confirm("Deseas eliminar al usuario", function() {

      // confirma eliminar
      $.post('tu_url_para_eliminar.php', { becarioId: id }, function(data) {
        // se eliminó correctamente
        alertify.success('Usuario eliminado');
      }).fail(function() {
        // ocurrió un error
        alertify.error('Error al eliminar usuario');
      });

    });
  }

});

Method $ .post ()

Method $ .ajax () (full version of $.post() or $.get() )

    
answered by 20.10.2018 / 06:47
source
1

Greetings I leave the code:

<!-- JavaScript -->
<script src="//cdn.jsdelivr.net/npm/[email protected]/build/alertify.min.js"></script>

<!-- CSS -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/build/css/alertify.min.css"/>
<form action="" onsubmit="return false" name="miFormulario">
<input type="text">
<input type="submit" value="Grabar Datos">
</form>
<script>
$(document).on('submit', 'form', function(e){

    alertify.confirm('Confirm Title', 'Confirm Message',
    function(){
    //submit
    document.miFormulario.submit();
    alertify.success('Ok') 
    },
    function(){ 
    alertify.error('Cancel')

    });
});


</script>

Explanation , inside the form:

<form action="" onsubmit="return false" name="miFormulario">

I stop the submit to control it by javascript, an additional name is assigned to the form that serves for once the action is confirmed, I send the form or I do the submit.

//submit
document.miFormulario.submit();

with that code what I do once confirmed the action of the alert I send the form or I force the submit.

'Confirm Title', 'Confirm Message',

that part serves to put the title of the alert and your message:

I hope you serve, I leave the link of the official documentation for more information. Documentation-Confirm

    
answered by 19.10.2018 в 22:39