无需刷新页面的自动刷新表 PHP MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8128877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:01:12  来源:igfitidea点击:

Auto refresh table without refreshing page PHP MySQL

phpmysqlhtml-tablechatreload

提问by Albert Renshaw

I have a very simple chat system I've built using PHP and MySQL (this is my second day ever using these languages) and I am wondering if there is any way to auto refresh the table data I'm pulling from my database and loading into an html table via PHP without having something like Javascript go and reload the whole web page... just reloading the html table with the data in it that PHP filled it up with.... Does that make sense?

我有一个使用 PHP 和 MySQL 构建的非常简单的聊天系统(这是我使用这些语言的第二天),我想知道是否有任何方法可以自动刷新我从数据库中提取并加载的表数据通过 PHP 进入一个 html 表,而无需像 Javascript 这样的东西去重新加载整个网页......只需重新加载带有 PHP 填充数据的 html 表......这有意义吗?

Here is my code if it helps (for /chat.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>

回答by zrvan

The technique is called AJAX and one of the easiest libraries to add to the project would be jQuery. I assume your issue isn't with JavaScript, but with the idea of reloading the entire page.

该技术称为 AJAX,最容易添加到项目中的库之一是jQuery。我认为您的问题不在于 JavaScript,而在于重新加载整个页面的想法。

UPDATEBecause I'm such a nice guy ;) This should work, more or less, I haven't tried it out, so there might be a typo or two:

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

<?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()); 

$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>

As for coding techniques, you might want to look into SQL-injections and maybe writing cleaner HTML, but I'm sure you'll get there :)

至于编码技术,您可能想研究 SQL 注入并编写更干净的 HTML,但我相信您会做到的 :)

回答by Kae Verens

the only way you would do it without javascript is to use an iframe for the chat interface, and a meta refresh.

没有 javascript 的唯一方法是使用 iframe 作为聊天界面和元刷新。

but why not use javascript?

但为什么不使用javascript?