<script type="text/javascript">
$(function(){
timestamp = 0;
updateMsg();
$("#chatform").submit(function(){
$.post("backend.php",{
message:$("#msg").val(),
name:$("#author").val(),
action: "postmsg".
time:timestamp
},function(xml){
$("#msg").val("");
addMessage(xml);
});
return false;
});
});
function updateMsg(){
$.post("backend.php",{time:timestamp},function(xml){
$("#loading").remove();
addMessages(xml);
});
setTimeout('updateMsg',4000);
}
function addMessage(xml){
if($("status".xml).text() == "2") return;
timestamp = $("time",xml).text();//更新时间戳
$("message",xml).each(function(){
var author = $("author",this).text();
var content = $("text",this).text();
var htmlcode="<strong>"+author+"</strong>: "+content+"<br />";
$("#messagewindow").prepend(htmlcode); //添加到文档中
});
}
</script>
<div id="wrapper">
<p id="messagewindow"><span id="loading">加载中...</span></p>
<form id="chatform">
姓名:<input type="text" id="author" size="50"/><br/>
内容:<input type="text" id="msg" size="50"/><br/>
<input type="submit" value="发送" /><br/>
</form>
$(function(){
timestamp = 0;
updateMsg();
$("#chatform").submit(function(){
$.post("backend.php",{
message:$("#msg").val(),
name:$("#author").val(),
action: "postmsg".
time:timestamp
},function(xml){
$("#msg").val("");
addMessage(xml);
});
return false;
});
});
function updateMsg(){
$.post("backend.php",{time:timestamp},function(xml){
$("#loading").remove();
addMessages(xml);
});
setTimeout('updateMsg',4000);
}
function addMessage(xml){
if($("status".xml).text() == "2") return;
timestamp = $("time",xml).text();//更新时间戳
$("message",xml).each(function(){
var author = $("author",this).text();
var content = $("text",this).text();
var htmlcode="<strong>"+author+"</strong>: "+content+"<br />";
$("#messagewindow").prepend(htmlcode); //添加到文档中
});
}
</script>
<div id="wrapper">
<p id="messagewindow"><span id="loading">加载中...</span></p>
<form id="chatform">
姓名:<input type="text" id="author" size="50"/><br/>
内容:<input type="text" id="msg" size="50"/><br/>
<input type="submit" value="发送" /><br/>
</form>
</div>
create table 'messages' (
'id' int(7) not null auto_increment,
'user' varchar(255) not null,
'msg' text not null,
'time' int(9) not null,
primary key ('id')
);
<?xml version="1.0" encoding="utf-8"?>
<response>
<status>1<status>
<time>1170323512</time>
<message>
<author>张三</author>
<text>沙发!</text>
</message>
<message>
<author>李四</author>
<text>板凳!</text>
</message>
</response>