php - 每隔几秒刷新一次从 MYSQL 填充的 textarea
问题描述
我正在开发一个小的 PHP MYSQL 聊天应用程序。我需要每隔几秒钟刷新一次聊天窗口中的数据,而无需重新加载整个页面。我知道我必须使用 Javascript 或 Jquery 来完成此操作,但我真的不太了解它们。
下面是创建聊天窗口并从数据库中填充它的代码。
非常感谢,感谢帮助。
<textarea id="screen" name="screen" style="width:100%;height:300;resize:none">
<?php
$sql="SELECT * FROM `$tbl_5` WHERE advertid='$advertid' ORDER by id ASC";
$result = mysqli_query($dbconn, $sql);
while ($row2=mysqli_fetch_row($result))
{
echo $row2[5]."\n";
}
?>
</textarea>
编辑:我尝试使用 AJAX
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
sendRequest();
function sendRequest(){
$.ajax({
type: 'POST',
url: '/chatrefresh.php',
data: parameters,
success: function(response){
$('#chatwindow').html(response);
},
error: function(response){
alert(response);
},
complete: function() {
setInterval(sendRequest, 5000);
}
});
};
});
</script>
<table id="chatwindow" name="chatwindow"><tr><td></td></tr></table>
Chatrefresh.php
<textarea id="screen" name="screen" style="width:100%;height:300;resize:none">
<?php
include_once('session.php');
include('config.php');
$advertid=$_SESSION['advertid'];
$sql="SELECT * FROM `$tbl_5` WHERE advertid='$advertid' ORDER by id ASC";
$result = mysqli_query($dbconn, $sql);
while ($row=mysqli_fetch_row($result))
{
echo $row[5]."\n";
}
?>
</textarea>
解决方案
推荐阅读
- sql - 使用多个表插入到一个表中
- c++ - 创建 std::array 的动态 std::array
- node.js - 承诺在nodejs中没有以同步方式执行?
- c# - OpenOfficeXML 在 Excel 工作表中插入额外的行 (X++/C#)
- angular - Angular 6 @HostBinding('class') 和 @HostBinding('attr.class') 有什么区别
- java - 无法显示 Json 数据
- html - 如何将 DIV 在另一个 DIV 的 RHS 的剩余空间中居中
- java - 允许在电子邮件验证中使用破折号
- laravel - 如何仅在 laravel 中获取当前用户数据
- node.js - Autodesk 查看 MongoDB 数据库中的保存标记