Jokermario
Jokermario

Reputation: 23

Showing a confirm dialog before deleting row in a database

Please how do i show a confirm delete dialog button. so that if the user clicks on yes it will proceed with the deletion or else it won't delete the entry.

This is my code that works fine, but i do not know how to make it pop the "are you sure you want to delete dialog"

The HTML button

 <a class='btn btn-danger delete_data' id="<?php echo $matched1; ?>">Blast</a>

 <script type="text/javascript">
        $(document).ready(function(){
            $(".delete_data").click(function(){
                var del_id = $(this).attr('id');
                $.ajax({
                    type: 'POST',
                    url: 'delete.php',
                    data: 'delete_id='+del_id,
                    success: function(data){
                        if(!data){
                            window.location.replace("memberpage.php");
                        }
                        else{
                            alert('failed');
                        }
                    }
                });
            });
        });
    </script>

delete.php

$username = $_POST['delete_id'];
    $stmt = $db->prepare("DELETE FROM users WHERE username='".$username."'");
    $stmt->execute();

Thanks a you help me out

Upvotes: 2

Views: 776

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You can use confirm() like below:-

$(".delete_data").click(function(){
    var choice = confirm('Do you really want to delete this record?');
        if(choice === true) {
            var del_id = $(this).attr('id');
            $.ajax({
                type: 'POST',
                url: 'delete.php',
                data: 'delete_id='+del_id,
                success: function(data){
                    if(!data){
                        window.location.replace("memberpage.php");
                    }
                    else{
                        alert('failed');
                    }
                }
            });
        }else{
            return false;
        }
});

reference:- https://www.w3schools.com/js/js_popup.asp

Upvotes: 1

Thiago Elias
Thiago Elias

Reputation: 939

You may use the Javascript confirm. I've changed your code to show how it is:

<a class='btn btn-danger delete_data' id="<?php echo $matched1; ?>">Blast</a>

     <script type="text/javascript">
        $(document).ready(function(){
            $(".delete_data").click(function(){
                var del_id = $(this).attr('id');
                if (confirm("Do you really want to remove ?")) {
                    $.ajax({
                        type: 'POST',
                        url: 'delete.php',
                        data: 'delete_id='+del_id,
                        success: function(data){
                            if(!data){
                                window.location.replace("memberpage.php");
                            }
                            else{
                                alert('failed');
                            }
                        }
                    });
                }
            });
        });
    </script>

In this way, you're asking the user if he wants to remove data. Javascript confirm returns true if user confirm the action. If he clicks in cancel, nothing will be executed.

If you don't like the default Browser UI, you have options, like jQuery UI, or if you're using Bootstrap, you may use Bootbox, which is user friendly and easy to use.

Upvotes: 1

Related Questions