聊天室基本小步骤

由index.php组成主页面,建立基本框架

总体的php部分由chatindex部分构成,

检测用户名用checkuser.php。得到内容有getcontent.php。总体传递用send.php.

index.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>河软CSDN乐知学院 聊天室</title>
<style>
  table{
   margin:auto auto;
   border:#00F 1px dashed;
   border-collapse:collapse;
   font-size:14px;
   }
  tr,td{
   border:#CCC 1px solid;}
</style>
</head>

<body><br><br><br><br><br>
<form action="../20120306-ajax-chatroom/login-check.php"     method="post">
<table width="300" border="0">
  <tr>
    <td colspan="2" align="center">欢迎使用CSDN学院聊天室</td>
  </tr>
  <tr>
    <td align="right">用户名:</td>
    <td><input type="text" name="username"  /></td>
  </tr>
  <tr>
    <td align="right">密码:</td>
    <td><input type="password" name="password"  /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="登录" />&nbsp;&nbsp;<input type="reset" / value="重置"></td>
  </tr>
</table>
</form>
</body>
</html>

chatindex.php内容

<?php
include 'checkUser.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>欢迎进入聊天室~</title>
<style>
  #chatContent{
   width:500px;
   height:300px;
   background-color:#FFC;
   text-align:left;
   overflow:scroll;}
  #chatContent span{
   font-size:12px;}
  table{
   margin:auto auto;
   border:#00F 1px dashed;
   border-collapse:collapse;
   font-size:14px;
  }
  tr,td{
   border:#CCC 1px solid;
   text-align:center;}
  #send_btn{
   width:65px;
   height:65px;
   color:#00F;}
</style>
<script language="javascript" type="text/javascript" src="../../include/ajaxUtil.js"></script>

<script language="javascript" type="text/javascript" src="../../include/fckeditorUtil.js">
</script>
<script language="javascript" type="text/javascript">
//发送信息处理---------------------------------------------------
//发送用户说的内容
function sendMsg(){
  
   var url="sendMsg.php";
   //调用方法,获取fck的值
   var acontent=getEditorHtmlContents('sendMsg',true);
   //alert(acontent);
 var params="nr="+encodeURI(acontent)+"&lis="+encodeURI($$('onlineUser').value); 
   //ajax请求
 get(url,params,processSendMsg);
 
}

function processSendMsg(xhr){
 
 if(xhr.responseText){
  
 //当发送成功后,清空输入框的内容
 SetEditorContents('sendMsg',"");
 
 }else{
   alert("发言失败"); 
 }
}
//--------------处理结束--------------------------
var maxid=0;
//获取内容,并且显示出来
function getContent(){
 
  var url="getContent.php";
  var params="maxid="+maxid;
  get(url,params,processGetContent);

}

function processGetContent(xhr){
 
 //把json格式的字符串转化为对象
 var rows=eval("("+xhr.responseText+")");
 
 var conStr=$$("chatContent").innerHTML;
 for(var i=0;i<rows.length;i++){
  
  //把最大id值赋值给maxid
  maxid=parseInt(rows[i].id);
  
  //封装字符串
  conStr+="<span>&nbsp;<b><font color='blue'>"+rows[i].speaker+"</font></b>&nbsp;["+rows[i].theip+"]&nbsp;在&nbsp;<font color='green'>"+rows[i].create_time+"</font>&nbsp;说</span>:<br>&nbsp;&nbsp;"+rows[i].content+"<br>";
  
  
 }
 
 //将内容显示到div中
 $$("chatContent").innerHTML=conStr;
}


setInterval("getContent()",2000);

</script>

 

</head>

<body>
<table width="600" border="0">
<caption>ajax+json+js+dom+php+mysql实现超强聊天室 河软CSDN乐知学院</caption>
  <tr>
    <td>欢迎<?php echo $_SESSION['username']; ?>,进入聊天室</td>
    <td>在线用户列表:</td>
  </tr>
  <tr>
    <td><div id="chatContent"></div></td>
    <td>
    <select id="onlineUser" size="20" style="width:100px;">
      <option>所有人</option>
      <option>悟空</option>
      <option>唐僧</option>
      <option>八戒</option>
      <option>观音</option>
    </select>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
    <?php

//引用FCKeditor.php这个文件,基本的类和数据结构都在这里
include ("fckeditor/fckeditor.php");
//创建FCKeditor对象的实例。myFCKeditor即提交后,接收数据页面 _POST['content']使用
$FCKeditor = new FCKeditor("sendMsg");
//FCKeditor所在的位置,这里它的位置就是'FCKeditor' 文件夹
$FCKeditor -> BasePath='./fckeditor/';
//工具按钮设置
$FCKeditor -> ToolbarSet="ChatEditor";
//设置它的宽度
$FCKeditor -> Width='500px';
//设置它的高度
$FCKeditor -> Height='100px';
//生成
$FCKeditor -> Create();

?>
   
   
   
   
   
    </td>
    <td><input id="send_btn" type="button" value="发送" οnclick="sendMsg()" /></td>
  </tr>
</table>

</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值