Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sweet alert redirects before the user clicks OK

I'm using SweetAlert2 and want to redirect the user to a different page, BUT after the success alert and the user clicks OK.

this one:

    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this imaginary file!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    },
    function() {
        swal(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        );
    });

like this:

function deleteEvent(id)
{
    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this event!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    }).then(function() {
        swal(
          'Deleted!',
          'Your event has been deleted.',
          'success'
        );
            $.ajax({
                url: '/events/'+id,
                type: "DELETE",
                data:{ _token: "{{csrf_token()}}" },
                success: function() {
                    location.href = '/events';
                }
            });
    });
}

But when the user clicks the confirm button to delete....this popup enter image description here

goes right away because of my redirect and the user doesn't even get the chance to click the OK button...

How can i make the redirect AFTER the user clicks the OK on this image?

like image 964
lewis4u Avatar asked Nov 02 '25 18:11

lewis4u


1 Answers

I finally solved this on my own:

function deleteEvent(id)
{
    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this event!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    }).then(function() {

        $.ajax({
            url: '/events/'+id,
            type: "DELETE",
            data:{ _token: "{{csrf_token()}}" }
        }).done(function() {

            swal({
                title: "Deleted", 
                text: "Event has been successfully deleted", 
                type: "success"
            }).then(function() {
                location.href = '/events';
            });
        });
    });
}
like image 169
lewis4u Avatar answered Nov 04 '25 07:11

lewis4u