1

Am using SweetAlert to do an AJAX call when user want to delete an account.But isn't working below is my code. I want to also add confirm and cancel button but I don't know how to use SweetAlert very well.

Any help will be appreciated.

I'm using CDN of SweetAlert.

https://unpkg.com/sweetalert/dist/sweetalert.min.js from https://sweetalert.js.org/

 swal({
   closeOnClickOutside: false,
   icon: "warning",
   title: 'Do you want to remove your account?',
   text: 'This action can not be undo',
   showConfirmButton: false,
   closeOnConfirm: false,
   showSpinner: true
 },function () {
   $.ajax({
     url: "delete_account.php",
     method: "POST",
     data: {
       id: 5
     },
     success: function () {
       swal("Deleted!", "Successfully deleted", "success");
     }
   });
 });
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Iamgisnet
  • 171
  • 3
  • 3
  • 12
  • Have you tried reading any of the documents for `SweetAlert`? https://sweetalert.js.org/docs/ `CTRL` + `F` -> `confirm` simple keyword search. I'm sure you will find what you need within the documentation, it requires time and reading with some testing. – NewToJS Sep 22 '17 at 04:27
  • Thanks for reply – Iamgisnet Sep 22 '17 at 04:49

3 Answers3

1

The ajax call goes on click of confirm only.

   function deleteorder(orderid) {
        swal({
          title: "Are you sure?", 
          text: "Are you sure that you want to cancel this order?", 
          type: "warning",
          showCancelButton: true,
          closeOnConfirm: false,
          confirmButtonText: "Yes, cancel it!",
          confirmButtonColor: "#ec6c62"
        }, function() {
            $.ajax(
                    {
                        type: "post",
                        url: "/admin/delete_order.php",
                        data: "orderid="+orderid,
                        success: function(data){
                        }
                    }
            )
          .done(function(data) {
            swal("Canceled!", "Your order was successfully canceled!", "success");
            $('#orders-history').load(document.URL +  ' #orders-history');
          })
          .error(function(data) {
            swal("Oops", "We couldn't connect to the server!", "error");
          });
        });
       }

Answer from here

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
1

You can call ajax on the confirmation of sweet alert as follows.

swal({
    title: "Are you sure to delete this  of ?",
    text: "Delete Confirmation?",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Delete",
    closeOnConfirm: false
  },
  function() {
    $.ajax({
        type: "post",
        url: "url",
        data: "data",
        success: function(data) {}
      })
      .done(function(data) {
        swal("Deleted!", "Data successfully Deleted!", "success");
      })
      .error(function(data) {
        swal("Oops", "We couldn't connect to the server!", "error");
      });
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
Jino Shaji
  • 1,097
  • 14
  • 27
0

you can try like below code.

setTimeout(function () { 
swal({
  title: "Wow!",
  title: 'Do you want to remove your account?',
   text: 'This action can not be undo',
  confirmButtonText: "OK"
},
function(isConfirm){
  if (isConfirm) {
    $.ajax({
     url: "delete_account.php",
     method: "POST",
     data: {
       id: 5
     },
     success: function () {
       swal("Deleted!", "Successfully deleted", "success");
     }
   });
  }
}); }, 1000);
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
chirag satapara
  • 1,947
  • 1
  • 15
  • 26