首页 > 解决方案 > 每隔几秒刷新一次从 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>

标签: phpjquerymysqlajaxtextarea

解决方案


推荐阅读