我有一个非常简单的聊天系统,我使用PHP和MySQL(这是我使用这些语言的第二天),我想知道是否有任何方法来自动刷新表数据我从我的数据库中拉出并通过PHP加载到HTML表中,而没有像JavaScript一样的东西去重新加载整个网页...只需重新载入PHP表格中的数据就可以使用PHP填充它.. .. 那有意义吗?自动刷新表而不刷新页面PHP MySQL
这里是我的代码,如果它有助于(对/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>
PHP只是服务器端,所以你需要的东西像Javascript运行PHP脚本并重新填充表格。你可以使用Jquery的$ .ajax函数填充一些内容I.E.顺便说一句,在这个例子中的DIV - http://api.jquery.com/jQuery.ajax/ –