Delete a record showing confirmation

0

What kind of friends do I have this function in jQuery that deletes a record from a dataTable, what I need is that a confirmation be shown when I click on the delete button in the table, so that it appears if you really want to delete it and another cancel button, this is my code:

$(document).on("click", ".btn-delete", function(e){
    e.preventDefault();
    url = $(this).attr("href");
    $.ajax({
        url: url,
        type: "POST",
        success: function(resp){
            window.location.href = base_url + resp;
        }
    });
});   

Something like this sweetalert:

    
asked by WilsonicX 06.09.2018 в 02:47
source

2 answers

1

Here is the solution with SweetAlert I hope it serves you, it served me well, thanks to those who showed interest in my question, greetings.

    $(document).on("click", ".btn-delete", function(e){
    e.preventDefault();
    url = $(this).attr("href");
    swal({
            title:"Esta seguro que desea eliminar este registro?",
            text: "Esta operacion es irreversible",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            buttonsStyling: false,
            confirmButtonText: "Eliminar",
            cancelButtonText: "Cancelar",
            closeOnConfirm: false,
            showLoaderOnConfirm: true,
        },
        function(isConfirm){
            if(isConfirm){
                $.ajax({
                    url: url,
                    type: "POST",
                    success: function(resp){
                        window.location.href = base_url + resp;
                    }
                });
                }
            return false;
        });
    });

    
answered by 06.09.2018 / 04:58
source
2

You can use something like this

<script>
$(document).on("click", ".btn-delete", function(e){
    e.preventDefault();
    url = $(this).attr("href");
    if(confirm("Esta seguro de eliminar?")){
        $.ajax({
            url: url,
            type: "POST",
            success: function(resp){
                window.location.href = base_url + resp;
            }
        });
    }
    return false;
});   
</script>}

Greetings:)

    
answered by 06.09.2018 в 03:55