加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

自动刷新表没有刷新页面PHP MySQL

发布时间:2020-12-13 21:44:05 所属栏目:PHP教程 来源:网络整理
导读:我有一个非常简单的聊天系统,我用 PHP和MySQL构建(这是我第二天使用这些语言),我想知道是否有任何方法可以自动刷新我从数据库中提取的表数据并加载通过PHP进入一个html表,没有像 Javascript这样的东西去重新加载整个网页…只需重新加载html表格中的PHP填充它
我有一个非常简单的聊天系统,我用 PHP和MySQL构建(这是我第二天使用这些语言),我想知道是否有任何方法可以自动刷新我从数据库中提取的表数据并加载通过PHP进入一个html表,没有像 Javascript这样的东西去重新加载整个网页…只需重新加载html表格中的PHP填充它的数据….这有意义吗?

这是我的代码,如果它有帮助(对于/chat.php)

<html><head></head><body><center>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" name="submitButton"/> <a href="http://www.****.com/chat.php"><button name="Refresh Chat">Refresh Chat</button></a>
</form>
<div style="width:100%;">

<?php

$host="****";
$user="****";
$password="****";

$cxn = mysql_pconnect ($host,$user,$password);

mysql_select_db("defaultdb",$cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ipaddress = getenv(REMOTE_ADDR);
}

$message = nl2br(strip_tags(nl2br($_POST["message"])));

if (isset($_POST['submitButton'])) { 
    if ($message != "") {
        mysql_query("INSERT INTO ChatTest (ID,TimeStamp,Message) VALUES ('$ipaddress',NOW(),'$message')");
    }
    header('Location: chat.php');
}

$message = "";

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 
 Print "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
        "; 
 Print "<tr>"; 
 Print "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
 while($info = mysql_fetch_array( $data )) { 
 Print "
        <tr>"; 
    Print " <td>".$info['ID'] . "</td> "; 
    Print " <td>".$info['TimeStamp'] . " </td>";
    Print " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
            "; 
 } 
 Print "</table>"; 

mysql_close($cxn);


?>

</div></center></body></html>

解决方法

该技术称为AJAX,添加到项目中的最简单的库之一是 jQuery.我假设您的问题不是使用JavaScript,而是考虑重新加载整个页面.

UPDATE
因为我是一个很好的人;)这应该工作,或多或少,我没有尝试过,所以可能有一两个错字:

<?php

$host="****";
$user="****";
$password="****";

$cxn = mysql_pconnect ($host,$cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}

$message = nl2br(strip_tags(nl2br($_POST["message"])));
if (isset($_POST['submitButton'])) { 
if ($message != "") {
mysql_query("INSERT INTO ChatTest (ID,'$message')");
}
header('Location: chat.php');
}

$message = "";

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 

$tbl = '';
$tbl .= "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
"; 
$tbl .= "<tr>"; 
$tbl .= "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
while($info = mysql_fetch_array( $data )) { 
$tbl .= "
<tr>"; 
$tbl .= " <td>".$info['ID'] . "</td> "; 
$tbl .= " <td>".$info['TimeStamp'] . " </td>";
$tbl .= " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
"; 
} 
$tbl .= "</table>"; 

mysql_close($cxn);

if (isset ($_GET['update']))
{
    echo $tbl;
    die ();
}

?>
<html><head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
</head><body><center>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" name="submitButton"/> <a href="http://www.****.com/chat.php"><button name="Refresh Chat">Refresh Chat</button></a>
</form>
<div id="messages" style="width:100%;">
  <?php echo $tbl; ?>
</div></center>
<script type="text/javascript">
$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#messages').load ('chat.php','update=true');
    },1000);
});
</script>
</body></html>

至于编码技术,您可能希望研究SQL注入并编写更清晰的HTML,但我相信您会到达那里:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读