嘿伙计们,我手头有一个特定的问题非常困难。我正在尝试创建一个使用 AJAX 获取帖子的数据库驱动的聊天室。但是,当调用使用 AJAX 获取数据时,我收到未定义索引警告。我使用 setinterval() 函数来调用包含 AJAX 的 javascript 函数。当达到 setinverval() 函数的时间时,我收到此警告。我该如何编码,以便当该变量为空并且达到 setinterval() 函数时,我停止收到警告?提前感谢所有愿意提供帮助的人。顺便说一句,这是我的第一篇文章,所以如果需要更多信息,请告诉我。再次感谢!!
这是chatRoom.php 文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery
/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "log.php",
cache: false,
success: function(html){
$("#chatbox").html(html);
//Insert chat log into the#chatboxdiv
var newscrollHeight = $("#chatbox").attr("scrollHeight")
- 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({scrollTop:newscrollHeight},'normal');
//Autoscroll to bottom of div
}
}
});
}
setInterval (loadLog, 1500); //Reload file every 2.5 seconds
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
</script>
</head>
<?php
include('functions.php');
session_start();
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $_SESSION['user']; ?></b></p>
<p class="logout"><a id="exit" href="#">Exit Chat</a></p>
<div style="clear:both"></div>
</div>
<div id="chatbox">
<?php
loadPosts();
?>
</div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
</body>
</html>
这是 php.post 文件
<?
include('functions.php');
session_start();
if(isset($_SESSION['user'])){
$text = $_POST['text'];
$uName = $_SESSION['user'];
$time = date("h:i A");
postComment($time, $uName, $text);
}
?>
函数.php 文件
<?php
include('connect.php');
function loadPosts(){
$query = mysql_query("SELECT * FROM chat") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo "No Posts Were Found";
}
while($post = mysql_fetch_assoc($query))
{
echo "(" . $post['time'] . ") <b>" . $post['user'] . "</b>: " . $post['text']
. "<br />";
}
}
function postComment($time, $userN, $userMessage){
mysql_query("INSERT INTO chat
Values(null, '$time', '$userN', '$userMessage')") or die(mysql_error());
}
function exitChat(){
header("Location: index.php");
}
?>
最后是新文件 log.php
<?php
include('functions.php');
session_start();
if(isset($_SESSION['user']))
loadPosts();
?>