Samy Tcheikman
Samy Tcheikman

Reputation: 160

Cant confirm delete with swal on laravel

hey guys i have a little probleme with swal condition when the user confirm the delete nothing happen (swal version 7.0.7)

there is the swal code

<form id="del_type" action="{{ route('admin.type.destroy', $type->id) }}" method="post"style="display: inline">
     {!! method_field('delete') !!}
     {{ csrf_field() }}
<button class="btn btn-danger  delete_type" type="submit" >Supprimer</button>
</form>
$(".delete_type").click( function (e) {
    e.preventDefault();
    var _this = $(this)

    //console.info(_this.parent().prop('action'))
    swal({
        title: "Attention",
        text: "Veuillez confirmer la suppression",
        type: "warning",
        showCancelButton: true,
        confirmButtonText: "Confirmer",
        cancelButtonText: "Annuler",
    }, function(result) {
        if(result) {
            $('#del_type').submit();
        } else {
            swal('cancelled');
        }
    });
});

when i click on the delete button it shows the swal with the confirm and cancel button but when you click on confirm nothing happens and there's no submit (and sorry for my english)

Upvotes: 2

Views: 514

Answers (2)

Roman Meyer
Roman Meyer

Reputation: 2872

According to official docs, you have to use promise and check result.value.

https://sweetalert2.github.io/v7.html

So, try to rewrite it a bit, like that:

Swal({
    title: "Attention",
    text: "Veuillez confirmer la suppression",
    type: "warning",
    showCancelButton: true,
    confirmButtonText: "Confirmer",
    cancelButtonText: "Annuler",
}).then((result) => {
    if (result.value) {
        $('#del_type').submit();
    } else {
        swal('cancelled');
    }
})

Upvotes: 2

Taki Elias
Taki Elias

Reputation: 127

You can do this without swal.

$(".delete_type").click( function (e) {
    if(!confirm('Do you want to Delete ?')){
            return false;
       }
  $('#del_type').submit();
});

Upvotes: 0

Related Questions