sweetalert not showing when deleting in laravel

Viewed 25

I am trying to implement sweet alert on my laravel project. But the delete method is working without the sweetalert.

in my blade file--

<div class="action-btn bg-danger ms-2">
    <a class="delete-confirm" href="{{ URL::to('/delete-customer/' . $customer->id) }}" type="button">
    <i class="ti ti-trash delete-confirm"></i></a>
</div>

in my controller--

public function delete($id)
    {
        $customer = Customer::findOrfail($id);
        
        $customer->delete();

        return redirect()->back()->with('success', __('Customer successfully deleted.'));
    }

For sweetalert I used this code--

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <script>
    $('.delete-confirm').on('click', function (event) {
        event.preventDefault();
        const url = $(this).attr('href');
        swal({
            title: 'Are you sure?',
            text: 'This record and it`s details will be permanantly deleted!',
            icon: 'warning',
            buttons: ["Cancel", "Yes!"],
        }).then(function(value) {
            if (value) {
                window.location.href = url;
            }
        });
    });
    </script>

when I click delete button it doesn't show the sweet alert. Can anyone explain what did I miss?

1 Answers

Try this

 $('body').on('click', '.delete-confirm', function() {
                var id = $(this).attr('data-id');
                Swal.fire({
                    title: 'Are you sure?',
                    text: "If You Remove A Referral You Will Not Be Able To Recover It!",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes'
                }).then((result) => {
                    if (result.value === true) {
                        var token = $("meta[name='csrf-token']").attr("content");
                        $.ajax({
                            type: "DELETE",
                            url: "/referrals/" + id,
                            data: {
                                "id": id,
                                "_token": token,
                            },
                            success: function(result1) {
                                Swal.fire({
                                    position: 'top-end',
                                    icon: 'success',
                                    title: 'Referral Removed ',
                                    text: "You Have Removed A Referral",
                                    showConfirmButton: false,
                                    timerProgressBar: true,
                                    timer: 1800
                                });
                                table.ajax.reload();
                            },
                            error: function(error) {
                                Swal.fire({
                                    position: 'top-end',
                                    icon: 'error',
                                    title: 'We have some error',
                                    showConfirmButton: false,
                                    timerProgressBar: true,
                                    timer: 1800
                                });
                            }
                        });
                    }
                });
            });
Related