2011-11-14 171 views
1

我有一個非常簡單的聊天系統,我使用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> 
+2

PHP只是服務器端,所以你需要的東西像Javascript運行PHP腳本並重新填充表格。你可以使用Jquery的$ .ajax函數填充一些內容I.E.順便說一句,在這個例子中的DIV - http://api.jquery.com/jQuery.ajax/ –

回答

5

該技術被稱爲AJAX,並且添加到項目中的最簡單的庫之一是jQuery。我假設你的問題不是用JavaScript,而是用重新加載整個頁面的想法。

UPDATE 因爲我是個好人;)這應該工作,或多或少,我還沒有嘗試過了,所以有可能是一個錯字或兩個:

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

至於編碼技術,你可能想看看SQL注入,也許編寫更乾淨的HTML,但我相信你會到達那裏:)

+0

是的,正是:)我會在平均時間看,謝謝! –

+0

謝謝! ahhhh :) –

1

你會做沒有JavaScript中的唯一方法是使用iframe的聊天界面,和元刷新。

但爲什麼不使用JavaScript?

+1

,查找「SQL注入」...你的聊天腳本有一個漏洞。 –

+0

對不起,混淆哈哈,我沒有問題,使用JavaScript ...我只是不想使用Javascript,將重新加載整個頁面...我只是想重新加載表數據,並保持在頁面上的一切它是(例如流式傳輸視頻,文本框中帶有文本,進度條,定時器等) –

+0

您不需要重新加載整個頁面。使用Ajax加載聊天數據並刷新界面。 –