从数据库获取数据没有页面刷新和没有按钮点击- AJAX PHP



我正在我的网站上创建一个CRUD函数。我正在使用Ajax立即加载数据,但它不会为我工作。

到目前为止,我看到他们需要点击一个按钮或设置一个时间间隔,但我不希望这样。我的代码中有什么错误或不足?这是我的HTML的样子

<div class="card-body">
<div id="blog_table">                    
</div>
</div>
<script>
$( document ).ready(function() {
$("#blog_table").load("functions/blog_list.php",{
});
});
</script>

<?php include "../../function.php";
$user_id = $_SESSION['id'];
$select_blogs = query("SELECT blog_id, blog_title, create_date FROM user_blog WHERE user_id = '{$user_id}'");
$result = confirm($select_blogs);
echo '<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Date Creasdawsdated</th>
</tr>
</thead>';
if(row_count($select_blogs) > 0){
while($row = fetch_assoc($select_blogs)){   
echo "<tr>
<td>".$row['blog_id']."</td>
<td>".$row['blog_title']."</td>
<td class='text-right py-0 align-middle'>
<div class='btn-group btn-group-sm'>
<button href='blog_view?id=".$row['blog_id']."'' class='btn btn-info'><i class='fas fa-eye'></i></button>
<button id='".$row['blog_id']."'' class='btn btn-danger' onclick='deleteFunction(this.id)'><i class='fas fa-trash'></i></button>
</div>
</td>
</tr>";
}   
}
?>

每当我点击删除按钮。它只是删除数据,但表不加载更改。

这是我的删除键的功能。

function deleteFunction(id) {
event.preventDefault();
var blog_id = id;
Swal.fire({
title: 'Are you sure you want to delete this blog?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
method: 'POST',
data: {'delete': true, 'blog_id' : blog_id },
url: 'functions/blog_delete.php',
success: function(data) {
}
});

Swal.fire(
'Deleted!',
'Your blog has been deleted.',
'success'
)

}
})
}

PHP删除函数文件。

<?php include "../../function.php";

$blog_id = escape_string($_POST['blog_id']);
$query = "DELETE FROM user_blog where blog_id = '{$blog_id}'";
$final_query = query($query);
confirm($final_query);
echo $final_query;
echo $query;
?>

当您将.load()函数添加到$(document).ready()时,您告诉jQuery加载页面一次-在页面加载时,当文档准备好了。这意味着,它不会再次更新,直到您再次运行该函数或刷新页面。

如果您想刷新数据,例如当您按下删除键时,请再次运行该功能,例如在您的deleteFunction()中。

在甜蜜警报后使用此功能。

Swal.fire(
'Deleted!',
'Your blog has been deleted.',
'success'
).then(function() {
$("#blog_table").load("functions/blog_list.php", {});
});

最新更新