[英]Ajax Delete from mysql with PHP using custom popup alert box
我有這個代碼,一切正常,但從 table 命令中刪除記錄不起作用。
這是頭部部分
<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>
這是正文代碼
<?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>
它的 deleteRecords.js 代碼
$(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....');
})
}
}
}
});
});
});
這是delete.php
<?php require('db_connect.php');
if($_POST['empid']) {
$sql = "DELETE FROM comments WHERE id='".$_POST['empid']."'";
if($sql) {
echo "Record Deleted";
}
}
?>
現在,問題是,當我單擊“刪除”圖標並在彈出警報中,我確認刪除時,該記錄暫時消失。 但它實際上並沒有從數據庫中刪除,當我刷新頁面時。 那條記錄又出現了。
我不明白為什么 delete.php 中的 delete 命令不起作用。 另外,我想對彈出式警報進行一些更改,當我單擊確認刪除時,會出現另一個彈出窗口,其中顯示已成功刪除的結果消息,我不想顯示此消息。 該怎么辦。 請幫幫我
IF 條件返回 TRUE,因為 $sql var 有內容,但您沒有執行查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.