やあみんな私は手に特定の問題で非常に困難な時間を過ごしています。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);
}
?>
Functions.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();
?>