日期:2014-05-17 浏览次数:20572 次
<html>
<head>
<?php
$username=$_GET['username'];
$username=trim($username);//去掉空格
session_start();
$loginuser=$_SESSION['loginuser'];
?>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script src="my.js"></script>
<script>
window.resizeTo(500,400);
window.setInterval("getMessage()",5000);
function getMessage(){
var myXmlHttpRequest=getXmlHttpObject();
if(myXmlHttpRequest){
var url="GetMessageControl.php";
var data="getter=<?php echo $loginuser; ?>&sender=<?php echo $username; ?>";
myXmlHttpRequest.open("post",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//重点。指定处理结果的 函数
myXmlHttpRequest.onreadystatechange=function (){
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
var mesRes=myXmlHttpRequest.responseXML;
var cons=mesRes.getElementsByTagName("con");
var sendTimes=mesRes.getElementsByTagName("sendTime");
window.alert(cons.length);
if(cons.length!=0){
for(var i=0;i<cons.length;i++){
$('mycons').value+="<?php echo $username; ?>对<?php echo $loginuser; ?>说:"+cons[i].childNodes[0].nodeValue+" "+sendTimes[i].childNodes[0].nodeValue;
}
}
}
}
}
myXmlHttpRequest.send(data);
}
}
function sendMessage(){
var myXmlHttpRequest=getXmlHttpObject();
if(myXmlHttpRequest){
var url="SendMessageControl.php";
//js中使用php数据
var data="con="+$('con').value+"&getter=<?php echo $username; ?>&sender=<?php echo $loginuser; ?>";
window.alert(data);
myXmlHttpRequest.open("post",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange=function (){
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
}
}
}
myXmlHttpRequest.send(data);
}
}
</script>
</head>
<center>
<h1>聊天室(您正在和<font color="red"><?php echo $username; ?></font>聊天)</h1>
<textarea cols="30" rows="10" id="mycons"></textarea><br>
<input type="text" style="width:200px" id="con">
<input type="button" value="send" onclick="sendMessage()">
</center>
</html>
<?php
include 'SqlHelper.class.php';
class MessageService{
//将数据信息添加到数据库
function addMessage($sender,$getter,$con){
$sql="insert into message (sender,getter,content,sendTime) values('$sender','$getter','$con',now())";
// file_put_contents("d:/test.txt","sql=".$sql."\r\n",FILE_APPEND);
$sqlHelper=new SqlHelper();
return $sqlHelper->execute_dml($sql);
}
//获取信息,并把数据组装好返回给客户端(聊天室)
function getMessage($getter,$sender){
$sql="select * from message w