How to use sweet alert before erasing MySQL rows

1

I'm trying to use sweet alert to confirm the intention to delete a row from the MySQL table, but I think I'm not sure how to do it.

Using alert simple I do this:

<a title='Remover linha' class= 'deletrow' href='deletrow.php?id=$id'><i class='fa fa-remove resultsfa5'></i></a>

In the file deletrow.php is MySQL to delete the line and before deleting the JS asks for confirmation:

$('.deletrow').on('click', function () {
    return confirm('Tem certeza que deseja apagar esta linha?');
});

But trying with sweet alert does not work, because when I put href inside the <a> tag (without href it works) it just flashes on the screen.

In this fiddle you can reproduce the problem. Just remove the href from within the tag that already works, otherwise it just blinks ... but then how do I point to the file where the query of MySQL is?

Below is the functional snippet (with button ):

      document.querySelector('.sweet-1').onclick = function(){
        swal("Nice alert!");
      };
<script src="//code.jquery.com/jquery-2.1.1.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://tristanedwards.me/u/SweetAlert/lib/sweet-alert.js"></script><linkhref="http://tristanedwards.me/u/SweetAlert/lib/sweet-alert.css" rel="stylesheet"/>
      <div class="row">
        <div class="col-sm-2 text-center">
          <p><button class="btn btn-primary sweet-1">Abrir alert</button></p>
        </div>
      </div>

So how can I use with <a href...> , or even using <button> where I include the rule to delete the line?

    
asked by anonymous 05.10.2015 / 05:57

1 answer

1

You can create a function that passes the ID as a parameter and clicking the "OK" button it redirects to the delete page! As below:

function confirmExcluir(id)
{
  swal({
      title: "Excluir",
      text: "Confirma a exclusão?",
      type: "error",
      showCancelButton: false,
      confirmButtonClass: 'btn-success',
      confirmButtonText: 'OK!',
      closeOnConfirm: false
   }, function () {
      window.location.href = 'deletrow.php?id=' + id;   
   });
}

And in the html it looks like this:

<a title='Remover linha' class='deletrow' onclick="confirmExcluir($id)" href='javascript:;'><i class='fa fa-remove resultsfa5'></i></a>
    
05.10.2015 / 15:35