Search code examples
sweetalert2

Sweet alert confirm delete on PHP app is not working


In my PHP app its normally deleted when click on the link/button

but when I implement sweet alert its not working or it just return false. I am using sweetalert2

Here is my button code

<a href="http://localhost/app/pages-role-list.php?delete_role=18" class="btn btn-sm btn-danger delete-confirm">delete</a>

My sweet init

$('.delete-confirm').on('click', function (event) {
    event.preventDefault();
    const url = $(this).attr('href');


    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'question',
        showCancelButton: true,
        confirmButtonText: 'Delete'

    },function(isConfirm){
        if (isConfirm) {
            return true;
            window.location.href = url;
        }else{
            return false;
        }
    })
});

Solution

  • Callback was not using correct syntax..

    Here is the working code:

    $('.delete-confirm').on('click', function (event) {
                event.preventDefault();
                const url = $(this).attr('href');
    
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    icon: 'question',
                    showCancelButton: true,
                    confirmButtonText: 'Delete'
                    
                }).then((result) => {
                    if (result.value) {
                        window.location.href = url;
                    } else if (result.dismiss === Swal.DismissReason.cancel) {
                        event.preventDefault();
                    }
                })
            });