Problem with sweetalert confirm link

2

Good morning,

I am somewhat confused since I tried to make a confirmation modal before going to a link (basically before deleting an item from the DB, asking if it is safe) using sweetalert2 and I get several errors that I can not / I can solve.

This is the code

  <a id="confirm" href="http://google.com">Visit Google!</a>

  <script>
  $("#confirm").click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    var linkURL = $(this).attr("href");
    warnBeforeRedirect(linkURL);
  });

  function warnBeforeRedirect(linkURL) {
    swal({
      title: "Leave this site?", 
      text: "If you click 'OK', you will be redirected to " + linkURL, 
      type: "warning",
      buttons: true
    }).then( function() {
      window.location.href = linkURL;
    });
  }
  </script>

With this code, if you give it to cancel when the modal appears, it still keeps taking you to the page. Does anyone know why?

On the other hand, I do not get to understand another rather strange concept. If I put the id confirm elsewhere, exactly in this code:

<ul class="page-breadcrumb">
<li> <a id="confirm" href="<?php echo $CRDomain; ?>dashboard">Inicio</a> <i class="fa fa-circle"></i> </li>
<li> <span>Usuarios</span> </li>
</ul>

The confirm does not work, nor show the modal or anything, it goes to the link directly.

Could someone help me?

---------------------------------

After using the response code, I still have a problem that I can not find the sense of:

<ul class="dropdown-menu" role="menu">
    <li>
        <a href="<?php echo $CRDomain; ?>profile?user=<?php echo $u['IDUser']; ?>"><i class="fa fa-eye" aria-hidden="true"></i> Ver Perfil </a>
    </li>
    <li>
        <?php if($u['Activacion'] == 1): ?>
        <a href="../assets/controllers/users.php?action=desactivaruser&userid=<?php echo $u['IDUser']; ?>"><i class="fa fa-toggle-off" aria-hidden="true"></i> Desactivar </a>
        <?php elseif($u['Activacion'] == 0): ?>
        <a href="../assets/controllers/users.php?action=activaruser&userid=<?php echo $u['IDUser']; ?>"><i class="fa fa-toggle-on" aria-hidden="true"></i> Activar </a>
        <?php endif; ?>
    </li>
    <li>
        <a href="javascript:;"><i class="fa fa-pencil" aria-hidden="true"></i> Editar </a>
    </li>

<script type="text/javascript">
$("#confirmBTN").click(function(e) {
e.preventDefault(); 
var linkURL = $(this).attr("href");
warnBeforeRedirect(linkURL);
});
function warnBeforeRedirect(linkURL) {
swal({
title: "¿Estas segur<?php get_Genero($_SESSION['CBSGenero']); ?>?",
text: "Una vez borrado el usuario, no podrá ser recuperado.",
type: "warning",
showCancelButton: true,
}).then(function(result) {
console.log(result);
if (result.value) {
window.location.href = linkURL;
}
});
}
</script>

    <li>
        <a id="confirmBTN" href="../assets/controllers/users.php?action=eliminaruser&userid=<?php echo $u['IDUser']; ?>"><i class="fa fa-trash" aria-hidden="true"></i> Eliminar </a>
    </li>
    <li class="dropdown-header">Permisos</li>
    <li>
        <a href="javascript:;"><i class="fa fa-eye" aria-hidden="true"></i> Ver o Editar </a>
    </li>
</ul>

This is the code. When I hit the "delete" button it does not show the confirmation screen, it just goes to the link directly. There is no id="confirmBTN" defined or anything like that, so it should work correctly ... I have reviewed the code and I do not see any apparent error ... so I'm a little lost.

Does anyone know why?

    
asked by Charlie Clewer 13.12.2017 в 14:29
source

2 answers

0

Using SweetAlert2 , I see 2 problems:

  • The buttons property is deprecated. Instead you should use showCancelButton
  • You are not validating the result.value in the function then . If result.value is true , only then you should redirect to the link.

Demo:

$("#confirm").click(function(e) {
  e.preventDefault(); // Prevent the href from redirecting directly
  var linkURL = $(this).attr("href");
  warnBeforeRedirect(linkURL);
});

function warnBeforeRedirect(linkURL) {
  swal({
    title: "Leave this site?",
    text: "If you click 'OK', you will be redirected to " + linkURL,
    type: "warning",
    showCancelButton: true,
  }).then(function(result) {
    console.log(result);
    if (result.value) {
      window.location.href = linkURL;
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/sweetalert2.all.js"></script>
<a id="confirm" href="https://google.com">Visit Google!</a>
    
answered by 13.12.2017 / 15:01
source
3

You have a failure in the structure of your swal , you are not enabling the cancel button, you must do it in the following way:

$("#confirm").click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    var linkURL = $(this).attr("href");
    warnBeforeRedirect(linkURL);
  });

  function warnBeforeRedirect(linkURL) {
    swal({
      title: "Leave this site?", 
      text: "If you click 'OK', you will be redirected to " + linkURL,
      showCancelButton: true,
      type: "warning"
    }, function(confirm){
      if(confirm){
        console.log('confirmado');
        window.location.href = linkURL;
      }
    })
  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="page-breadcrumb">
  <li>
    <a id="confirm" href="https://www.google.com">Inicio</a> <i class="fa fa-circle"></i> </li>
  <li> <span>Usuarios</span> </li>
</ul>

NOTE: The SOes snippet does not allow redirection to external links, if you run in your local environment should redirect without problems.

    
answered by 13.12.2017 в 14:36