使用javascript刷新html表
我目前正在开发一个简单的注释部分,一旦有新数据插入数据库,表就会加载。不是整页。我的问题是如何通过将查询加载到与javascript相同的文件中来使用javascript刷新表 comments.php使用javascript刷新html表,javascript,php,Javascript,Php,我目前正在开发一个简单的注释部分,一旦有新数据插入数据库,表就会加载。不是整页。我的问题是如何通过将查询加载到与javascript相同的文件中来使用javascript刷新表 comments.php <?php $getcomment = $conn->prepare("SELECT * FROM comments INNER JOIN document ON document.doc_groupid = comments.comment_doc INNER JOIN u
<?php
$getcomment = $conn->prepare("SELECT * FROM comments INNER JOIN document ON document.doc_groupid = comments.comment_doc INNER JOIN user ON user.user_matricno = comments.comment_user WHERE document.doc_id = :document ORDER BY comments.comment_timestamp DESC");
$getcomment->bindParam(':document', $document, PDO::PARAM_STR);
$document = $_GET['doc'];
$getcomment->execute();
readcomment = $getcomment->fetchAll();
$countc = $getcomment->rowCount();
foreach ($readcomment as $rc) {
echo
'
<table class="table table-responsive nowrap table-hover">
<tr>
<th style="border-top: #FFFFFF;pointer-events: none;">'. $rc["comment_subject"] .'</th>
<td class="pull-right" style="border-top: #FFFFFF;pointer-events: none;">by <strong>'. $rc["user_fname"].' '.$rc["user_lname"] .'</strong><small> '. time_elapsed_string($rc['comment_timestamp']).'</small></td>
</tr>
<tbody>
<tr>
<td colspan="2">'. $rc['comment_text'] .'</td>
</tr>
</tbody>
</table>';
}
?>
<div id="tablecomment"></div>
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tablecomment').load('comments.php', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
$(文档).ready(函数(){
刷新表();
});
函数刷新表(){
$('#tablecomment').load('comments.php',function(){
设置超时(刷新表,5000);
});
}
您需要使用ajax来实现这一点
这有点太复杂了,无法用一条评论来描述,而且还有很多很棒的教程。但是你想要做的和你已经在做的相似。设置一个timout来调用ajax(获取数据库数据)并用它刷新表
注意避免竞争条件,即前一个请求的数据在新请求之后到达