Hello friends!
I want the script when user clicks the Delete botton to delete that post, a confirm message appears and ask if the user is sure to delete that post or not, if the user clicks the cancel button, nothing happen but if clicks the ok button, then the script delete that post.
this is the main code:

<?php
include('connection.php');
try {
    $sql = "SELECT ID, Title, Author, Content FROM Posts";
    $result = $conn->query($sql);
    echo "<h4>LOGGED IN AS: " . $_SESSION["Username"] . "</h4>";
    echo "<p id='err_msg'></p>";
    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";
    foreach($result as $row) {
        echo "<tr class='tr'><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a id='".$row['ID']."' class='btn_dlt' href='#'> delete </a> <br> <a href='edit-page.php?post=" . $row['ID'] . "'>Edit</a></td></tr>";
    }
    echo "</table>";
} catch(PDOException $e) {
    echo "Query error:".$sql . "<br>" . $e->getMessage();
}
$conn = null;
?>



<html>
<head>
<title>admin</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(".btn_dlt").click(function(){
        var btn_dlt = $(this); 
        $.ajax({
            type: "POST",
            url: "./delete-action.php", 
            data: { id: btn_dlt.attr("id") } 
        }).done(function( msg ) { 
            alert(msg);
            if(msg == "Success"){  
                btn_dlt.closest('.tr').remove();
            }else{
                $('#err_msg').html(msg);  err_msg field
            }
        });
    });
});
</script>


<style>
a {
#text-decoration:none;
color:black;
}

h4 {
margin-left: 500px;
}

table {
background:#f3faf1;
}
</style>
</head>
<body>
<hr>
<a href='writing-post.php'>Write new post</a><br><br>
<a href='user-view-page.php'>Log out</a>
</body>
</html>

And the delete-action.php file:

<?php
    include('connection.php');
    try {

        $id=$_POST['id'];
        $sql = "DELETE FROM Posts WHERE ID = '$id'";
        $conn->exec($sql);
        echo "Success"; // the word here must be same as ajax comparism
    } catch(PDOException $e) {
        echo "Query error:".$sql . "<br>" . $e->getMessage();
    }
    $conn = null;
?>

I have tried a lot to find the way how to create the dialog message but i still didn't find how to do that exactly..... I don't like Javascript at all :|
Any help is welcome.

Recommended Answers

All 2 Replies

The confirmation box is quite simple:

$(".btn_dlt").click(function(){

        if ( ! confirm('Are you sure you want to delete this?') ) {
            return;
        }

        var btn_dlt = $(this); 
        $.ajax({
            type: "POST",
            url: "./delete-action.php", 
            data: { id: btn_dlt.attr("id") } 
        }).done(function( msg ) { 
            alert(msg);
            if(msg == "Success"){  
                btn_dlt.closest('.tr').remove();
            }else{
                $('#err_msg').html(msg);  err_msg field
            }
        });
    });

Oh! How easy it was!!
Thank you @AleMonteiro!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.