Sweet Alert Удалить Подтверждение с помощью ссылки - PullRequest
0 голосов
/ 09 февраля 2019

Я использую PHP и Sweet предупреждение для подтверждения удаления.Проблема в том, что он удаляется перед отображением сладкого предупреждения.

Это мой HTML (который использует в нем PHP).

<div class="delete"><a onclick="return confirmation()" href="'.URLROOT.'/dashboards/delete_note/'.htmlspecialchars($note->note_id).'/'.$data['table'] .'"><i class="far fa-trash-alt"></i></a></div>

Это мое сладкое оповещение

function confirmation() {
swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

}

Проблема в том, что оно не показывает сладкое предупреждение, просто идет прямо по URL.Нужно ли делать форму и предотвращать подчинение или что-то в этом роде?

Ответы [ 2 ]

0 голосов
/ 09 февраля 2019

Попробуйте удалить ссылку из ссылки и обрабатывать ее только после того, как пользователь подтвердит щелчок в сценарии SweetAlert.

<div class="delete"><a onclick="return confirmation()"><i class="far fa-trash-alt"></i></a></div>

В вашем JavaScript:

function confirmation() {
swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {

    // TODO: Handle your delete url by ajax
    // ...

    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});
0 голосов
/ 09 февраля 2019

Проблема в том, что щелчок по элементу привязки имеет поведение по умолчанию.Вы можете использовать event.preventDefault() для предотвращения перенаправления и навигации вручную на основе вашей логики с помощью window.location.href='url'

<div class="delete"><a onclick="confirmation(event)" href="'.URLROOT.'/dashboards/delete_note/'.htmlspecialchars($note->note_id).'/'.$data['table'] .'"><i class="far fa-trash-alt"></i></a></div>

и в js

function confirmation(ev) {
ev.preventDefault();
var urlToRedirect = ev.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
console.log(urlToRedirect); // verify if this is the right URL
swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  // redirect with javascript here as per your logic after showing the alert using the urlToRedirect value
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});
}
...