hassan khosro
hassan khosro

Reputation: 189

How can I cancel confrim sweet alert

Hi I'am using sweet alert for removing a product from my website I want to use it with two option first is 'ok' and second is 'cancel'

but when I click on wherever on page it removes and cancel button does't work

here is my code

 $(document).on('click', '#removeCat', function(e) {
    e.preventDefault();
    var id = $(this).data('id');
    swal({
        title: "are u sure?",
        type: "error",
        confirmButtonClass: "btn-danger",
        confirmButtonText: "ok",
        cancelButtonText: "cancel",
        showCancelButton: true,
    })
    .then(function() {
    $.ajax({
        type: "post",
        url: "{{url('dashboard/shop/product-category/delete')}}",
        data: {
            id: id,
            "_token": $('#csrf-token')[0].content //pass the CSRF_TOKEN()
        },
        success: function(data) {
            var url = document.location.origin + "/dashboard/shop/product-category";
            location.href = url;
        }
    });
});

});

Upvotes: 0

Views: 626

Answers (1)

Gulshan S
Gulshan S

Reputation: 1094

Here is solution:

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonClass: "btn-danger",
  confirmButtonText: "Yes, delete it!",
  cancelButtonText: "No, cancel plx!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm) {
  if (isConfirm) {
    $.ajax({
        type: "post",
        url: "{{url('dashboard/shop/product-category/delete')}}",
        data: {
            id: id,
            "_token": $('#csrf-token')[0].content //pass the CSRF_TOKEN()
        },
        success: function(data) {
            var url = document.location.origin + "/dashboard/shop/product-category";
            location.href = url;
        }
    });
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.css" integrity="sha256-Z8TW+REiUm9zSQMGZH4bfZi52VJgMqETCbPFlGRB1P8=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css" integrity="sha256-zuyRv+YsWwh1XR5tsrZ7VCfGqUmmPmqBjIvJgQWoSDo=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.js" integrity="sha256-ZvMf9li0M5GGriGUEKn1g6lLwnj5u+ENqCbLM5ItjQ0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js" integrity="sha256-JirYRqbf+qzfqVtEE4GETyHlAbiCpC005yBTa4rj6xg=" crossorigin="anonymous"></script>

Upvotes: 1

Related Questions