[英]Refresh html table using javascript
我目前正在開發一個簡單的注釋部分,一旦在數據庫中插入了新數據,該表就會加載。 不是整個頁面。 我的問題是如何通過將查詢加載到與javascript相同的文件中來使用javascript刷新表格?
的comments.php
<?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>
您需要使用ajax來實現。
https://www.w3schools.com/xml/ajax_intro.asp
在單個注釋中描述太復雜了,那里有很多很棒的教程。 但是想要您想做的事情類似於您已經在做的事情。 設置一個timout來調用ajax(獲取數據庫數據)並使用它刷新表。
小心避免出現競爭情況,即先前請求中的數據在新請求之后到達
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.