Mian
Mian

Reputation: 161

Ajax Delete from mysql with PHP using custom popup alert box

I have this code, everything works fine but delete records from table command is not working.

Here is Head Section

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
</script>
<script type="text/javascript" src="http://www.phpzag.com/demo/delete-records-with-bootstrap-confirm-modal-using-php-mysql/script/bootbox.min.js"></script>

<script type="text/javascript" src="deleteRecords.js"></script>

Here is body code

<?php require('db_connect.php'); ?>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Comments</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM comments";
    $select = mysql_query($sql);
    while($rows = mysql_fetch_array($select)){
?>
<tr>
<td><?php echo $rows["usercom"]; ?></td>
<td>
<a class="delete_employee" data-emp-id="<?php echo $rows["id"]; ?>" href="javascript:void(0)">
<i class="glyphicon glyphicon-trash"></i>
</a></td>
</tr>
<?php } ?>
</tbody>
</table>

its deleteRecords.js code

$(document).ready(function(){
    $('.delete_employee').click(function(e){
        e.preventDefault();
        var empid = $(this).attr('data-emp-id');
        var parent = $(this).parent("td").parent("tr");
        bootbox.dialog({
            message: "Are you sure you want to Delete ?",
            title: "<i class='glyphicon glyphicon-trash'></i> Delete !",
            buttons: {
                success: {
                    label: "No",
                    className: "btn-success",
                    callback: function() {
                        $('.bootbox').modal('hide');
                    }
                },
                danger: {
                    label: "Delete!",
                    className: "btn-danger",
                    callback: function() {
                    $.ajax({
                        type: 'POST',
                        url: 'delete.php',
                        data: 'empid='+empid
                    })
                   .done(function(response){
                       bootbox.alert(response);
                       parent.fadeOut('slow');
                    })
                   .fail(function(){
                       bootbox.alert('Error....');
                    })
                }
            }
        }
    });
});

});

and here is delete.php

<?php require('db_connect.php');

if($_POST['empid']) {
$sql = "DELETE FROM comments WHERE id='".$_POST['empid']."'";
if($sql) {
echo "Record Deleted";
}
}
?>

Now, the issue is, When i click on Delete icon, and in popup alert, i confirm delete, the record disappear for the time being. but its not been deleted from the database actually, and when i refresh the page. that record appears again.

I don't understand why delete command in delete.php is not working. Also, I want a little change in popup alert, when i click on confirm delete, one more popup appears with the result message that Deleted Successfully I don't want to show this message. what to do. Plz Help me

Upvotes: 0

Views: 1360

Answers (1)

jonilgz
jonilgz

Reputation: 226

The IF condition are returning TRUE because $sql var has content, but your not executing the query.

Upvotes: 3

Related Questions