rezky
rezky

Reputation: 15

Laravel delete function with Sweet Alert

So I'm using sweet alert on laravel 5.7.

I need help as it seems that the function doesn't work after I click the sweet alert confirm button.

my html:

<button rel="{{ $mail->id }}"" rel1="delete-mail" href="javascript:" 
class="deleteMail btn btn-danger btn-xs">Delete</button></td>

my script:

    $(".deleteMail").click(function(){
    var id = $(this).attr('rel');
    var deleteFunction = $(this).attr('rel1');
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        confirmButtonText: 'Yes, delete it!',
        buttonsStyling: false

    },
    function(){
      window.location.href='/admin/'+deleteFunction+'/'+id;
    });

    });

Route:

public function delete($id = null){

    if(!empty($id)){
        Mail::where(['id'=>$id])->delete();
        return redirect()->back()->with('flash_message_success','Surat berhasil dihapus!!');
    }

}

I've tried window.location.href without sweet alert function and it workdc fine.

However, when I'm using sweet alert it doesn't work at all.

I can't find any error in console log. please help.

Upvotes: 1

Views: 986

Answers (2)

bokino12
bokino12

Reputation: 31

try

 $(".deleteMail").click(function(){
    var id = $(this).attr('rel');
    var deleteFunction = $(this).attr('rel1');
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        confirmButtonText: 'Yes, delete it!',
        buttonsStyling: false

    },function(isConfirm){
        alert('ok');
    });
    $('.swal2-confirm').click(function(){
        window.location.href='/admin/'+deleteFunction+'/'+id;
    });
});

Upvotes: 0

Delowar Hossain
Delowar Hossain

Reputation: 385

Try .then function. For example:

  $(".deleteMail").click(function(){
  var id = $(this).attr('rel');
  var deleteFunction = $(this).attr('rel1');
  swal({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      type: 'warning',
      showCancelButton: true,
      confirmButtonClass: 'btn btn-success',
      cancelButtonClass: 'btn btn-danger',
      confirmButtonText: 'Yes, delete it!',
      buttonsStyling: false

    }).then((isConfirm) => {


   if (isConfirm){
       window.location.href='/admin/'+deleteFunction+'/'+id;
     }
    });
    });

Upvotes: 1

Related Questions