我想做一个 jQuery 聊天,但我无法将聊天内容加载到聊天框.. 加载脚本(我使用会话登录,所以我需要将用户名传输到 JS)
<?php
include "protect/1.php";
print "<script type=\"text/javascript\">var name = '".$_SESSION['username']."'; </script>";
?>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ chat_update(name) });
setInterval("chat_update("+name+")",1000);
function chat_update(user) {
$.post("response.php",{ username:user },function(data){
$(".chatbox").html(data);
}
</script>
和 div:
<div class="chatbox"></div>
如果我打开 response.php(通过发送帖子值的表单),它会加载所有消息。所以很纠结。。
编辑:要添加response.php,也许问题就在那里,但我不这么认为..
<?php
include "functions.php";
include "sql.php";
$iQuery = mysql_query("SELECT * FROM chat ORDER BY id ASC");
while($row = mysql_fetch_array($iQuery)) {
if($row["to"] != null) {
if($_POST["username"] == $row["to"] || $_POST["username"] == $row["sender"])
echo "<span class=\"text\">".$row["date"]." | ".$row["sender"].": ".$row["message"]."</span><br>\n";
}
switch(getbyUser($_POST["username"],"level")) {
case 1: echo "<span class=\"text\">".$row["date"]." | ".$row["sender"].": ".$row["message"]."</span><br>\n"; break;
case 2: echo "<span class=\"text\">".$row["date"]." | ".$row["sender"].": ".$row["message"]."</span><a href=\"?page=admin/delmsg&msg=".$row["id"]."\"><img src=\"res/del.png\"></a><br>\n"; break;
case 3:
case 4: echo "<span class=\"text\">".$row["date"]." | ".$row["sender"].": ".$row["message"]."</span><a href=\"?page=admin/delmsg&msg=".$row["id"]."\"><img src=\"res/del.png\"></a><a href=\"?page=admin/editmsg&msg=".$row["id"]."\"><img src=\"res/edit.png\"></a><br>\n"; break;
}
}
mysql_free_result($iQuery);
?>