Han Shuping 氏が作成した Web チャット システムを真似してください。何が問題だったのかを見つけるのを手伝ってください
<br />*******chatRoom.php******<br /><html><br /><head><br /><?php<br />//接受open传递的用户名<br /> $username=$_GET['username'];<br /> $username=trim($username);<br /> session_start();<br /> $loginuser=$_SESSION['loginuser'];<br />?><br /><meta http-equiv="content-type" content="text/html;charset=utf-8"/><br /><script type="text/javascript" src="my.js"></script><br /><script type="text/javascript"><br /><br /> window.resizeTo(800,700);<br /> window.setInterval("getMessage()",3000);<br /><br /> function getMessage(){<br /> //创建一个xmlHttpRequest对象<br /> var myXmlHttpRequest=getXmlHttpObject();<br /> if(myXmlHttpRequest){<br /> <br /> var url="getMessageController.php";<br /> var data="getter=<?php echo $loginuser; ?>&sender=<?php echo $username; ?>";<br /> <br /> myXmlHttpRequest.open("post",url,true);<br /> myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br /> myXmlHttpRequest.onreadystatechange=function(){<br /> if(myXmlHttpRequest.readyState==4){<br /> if(myXmlHttpRequest.status==200){<br /> var mesRes=myXmlHttpRequest.responseXML;<br /> var cons=mesRes.getElementsByTagName("con");<br /> var sendtimes=mesRes.getElementsByTagName("sendtime");<br /> if(cons.length!=0){<br /> for(var i=0;i<cons.length;i++){<br /> $("mycons").value+="<?php echo $username;?>说:"+cons[i].childNodes[0].nodeValue+"___"+sendtimes[i].childNodes[0].nodeValue;<br /> <br /> <br /> }<br /> }<br /> }<br /> }<br /> }<br /> myXmlHttpRequest.send(data);<br /> }<br /> }<br /> function sendMessage(){<br /> //创建一个xmlHttpRequest对象<br /> var myXmlHttpRequest=getXmlHttpObject();<br /> if(myXmlHttpRequest){<br /> <br /> var url="sendMessageController.php";<br /> var data="con="+$('con').value+"&getter=<?php echo $username; ?>&sender=<?php echo $loginuser; ?>";<br /> <br /> myXmlHttpRequest.open("post",url,true);<br /> myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br /> myXmlHttpRequest.onreadystatechange=function(){<br /> if(myXmlHttpRequest.readyState==4){<br /> if(myXmlHttpRequest.status==200){<br /> //这里是返回信息,暂不需要<br /> <br /> }<br /> <br /> }<br /> <br /> }<br /> myXmlHttpRequest.send(data);<br /> }<br /> }<br /><br /></script><br /></head><br /><body><br /><center><br /><br /><h1>网页聊天室(<font color="blue"><?php echo $loginuser; ?></font>正在和<font color="red"><?php echo $username; ?></font>聊天)</h1><br /><textarea cols="60" rows="20" id="mycons"></textarea><br/><br /><input type="text" style="width:300px" id="con"/><br /><input type="button" value="发送" onclick="sendMessage()"/><br /></center><br /></body><br /></html><br /><br />*********getMessageController.php********<br /><br /><?php<br />require_once 'MessageService.class.php';<br />header("content-type:text/xml;charset=utf-8");<br />header("Cache-Control:no-cache"):<br /> $getter=$_POST['getter'];<br /> $sender=$_POST['sender'];<br /> <br /> $messageService=new MessageService();<br /><br /> $messageList=$messageService->getMessage($getter,$sender);<br /> <br /> echo $messageList;<br /><br /> ?><br /><br />*********MessageService.class.php********<br /><br /><?php<br /><br />require_once 'SqlHelper.class.php';<br />class MessageService{<br /><br /> function addMessage($sender,$getter,$con){<br />$sql="insert into messages (sender,getter,content,sendtime) values ('$sender','$getter','$con',now())";<br />$sqlHelper=new SqlHelper();<br />return $sqlHelper->execute_dml($sql);<br />}<br /><br /> function getMessage($getter,$sender){<br /> <br /> $sql="select * from messages where getter='$getter' and sender='$sender' and isGet=0";<br /> <br /> $sqlHelper=new SqlHelper();<br /><br /> $array=$sqlHelper->execute_dql2($sql);<br /> $messageInfo="<meses>";<br /> for($i=0;$i<count($array);$i++){<br /> $row=$array[$i];<br /> $messageInfo.="<mesid>{$row['id']}</mesid><sender>{$row['sender']}</sender><getter>{$row['getter']}</getter><con>{$row['content']}</con><sendtime>{$row['sendtime']}</sendtime>";<br /> }<br /> $messageInfo.="</meses>";<br /> $sqlHelper->close_connect();<br /> return $messageInfo;<br /> }<br /><br />}<br />?><br />
....<br />if(myXmlHttpRequest.status==200){<br /> var mesRes=myXmlHttpRequest.responseXML;<br /> if(mesRes.xml == '') { //如果不是 xml 对象<br /> alert(myXmlHttpRequest.responseText);//看一下究竟返回了什么<br /> return;<br /> }<br />....<br />