基于Jquery的Ajax聊天室程序

<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>


</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>











  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值