Ajax实现聊天
用Ajax发送请求,查询数据库是否有自己的数据,如果有自己的数据,就返回
前端页面
<head>
<meta charset="UTF-8">
<title>登录聊天室</title>
<?php
session_start();
$sender = $_SESSION['loginuser'];
$username = $_GET['username'];
?>
<meta name="name" content="content" charset="utf-8">
<script type="text/javascript">
function ismsg(){//是否有信息
var xhr=myAjax();
xhr.open('post','ismsg.php',true);
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
xhr.onreadystatechange=function (){
if(this.readyState==4){
$('mymess').innerHTML+=this.responseText;
}
}
var name=$('user').innerHTML;
//alert(name);
xhr.send('username='+name);
}
function sendMessage(){
ismsg();
var myXmlHttpRequest = myAjax();
//alert(myXmlHttpRequest);
if(myXmlHttpRequest){
var url = "sendMessage.php";
var data = "con="+$('con').value+"&getter=<?php echo $username;?>&sender=<?php echo $sender;?>";
//alert($('con').value);
myXmlHttpRequest.open('post',url,true);
myXmlHttpRequest.onreadystatechange = function(){
if(myXmlHttpRequest.readyState == 4){
//alert('das');
//document.getElementById('re').innerHTML=myXmlHttpRequest.responseText;
if(myXmlHttpRequest.status == 200){
//alert(200);
//alert(myXmlHttpRequest.responseText);
if(myXmlHttpRequest.responseText ==1){
var con = $('con').value;
$('mymess').innerHTML += "<br/>"+"<?php echo $sender;?> "+show()+"<br/>"+con+"\n"+"<br/>";
$('con').value = '';
//ismsg();
}
}
}
}
myXmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
myXmlHttpRequest.send(data);
}
}
//获取响应ID信息的公用函数
function $(id){
return document.getElementById(id);
}
//考虑浏览器兼容创造ajax对象
function myAjax(){
var myAjax = false;
if(window.XMLHttpRequest){
myAjax = new XMLHttpRequest();
}else{
myAjax = new window.Activexobject('Microsoft.XMLHttp');
}
return myAjax;
}
function show(){
var date = new Date();
var now = "";
now = date.getFullYear()+"-";
now = now + (date.getMonth()+1)+"-";
now = now + date.getDate()+" ";
now = now + date.getHours()+":";
now = now + date.getMinutes()+":";
now = now + date.getSeconds()+"";
return now;
}
</script>
</head>
<body>
<h1>聊天室(<span id='user'><?php echo $sender;?></span>正在与<?php echo $username;?>)聊天</h1>
<div id="re"></div>
<div style="width:500px; height:400px; overflow:auto; border:1px solid green; background=grey;" id="mymess"></div><br />
<input type="text" id="con" style="width:375px;">
<input type="button" value="发送" onclick="sendMessage();">
<input type="button" value="看看有没有信息" onclick="ismsg();"/>
</body>
</html>
ismsg.php
<?php
header("Content-type:text/html;charset=utf-8");
//获取所有未读的信息
require('./mysql.php');
$mysql=mysql::getIns();
$sql="select * from messages where isget=0 and geter='".$_POST['username']."'";
$rs=$mysql->getAll($sql);
$str='';
//echo 'haha';
if($rs){
foreach($rs as $v){
echo $v['sender'].' '.date('Y-m-d H:i:s').'<br/>'.$v['content'].'<br/>';
}
//echo $str; }
$sql="update messages set isget=1 where isget=0 and geter='".$_POST['username']."'";
$mysql->query($sql);
?>
sendMessage.php
<?php
header("Content-type:text/html;charset=utf-8");
$sender = $_POST['sender'];
$getter = $_POST['getter'];
$con = $_POST['con'];
//echo $con;
//echo $sender,$getter,$con;
echo add_message($sender,$getter,$con);
//file_put_contents('send.txt',$sender."-".$getter."-".$con."\t\n",FILE_APPEND);
function sql_con(){
$link = mysql_connect('localhost','root','111111');
if(!$link){
echo "连接失败".mysql_error();die;
}
//mysql_select_db('chatroom',$link);
//mysql_query('set names utf8');
return $link;
}
function add_message($sender,$getter,$con){
$conn=sql_con();
mysql_query('use chatroom',$conn);
mysql_query('set names utf8',$conn);
$sql = "insert into messages(sender,geter,content,sendtime) values('$sender','$getter','$con',now())";
//echo $sql;
$res = mysql_query($sql,$conn);
//print_r($res);
return $res;
}
?>
Ajax实现聊天的更多相关文章
- Ajax实现聊天室
Ajax实现聊天室 运行效果如下: 代码显示: var net=new Object();//编写构造函数net.AjaxRequest=function(url,onload,onerror,met ...
- 基于servlet和ajax的聊天室
(手贱点了更新发布时间,发布时间变成6-9...) 2017-5-20,在这个奇特的日子,我不再满足于在本地测试javaweb,于是在上腾讯云买了第一个云服务器,由于是学生认证,所以一个月只要10块钱 ...
- 使用PHP+ajax打造聊天室应用
方法1. comet http://www.xiumu.org/technology/the-php-notes-comet-long-connection-instance.shtml 这篇文章 ...
- ajax实现聊天室功能
需求如下: 先死后活. 需求分析,分析思路如图所示: 1.创建数据库 create database chat; create table messages( id int unsigned prim ...
- Ajax技术实战操练课堂学习笔记
ajax是什么 ? ajax(asynchronouse javascript and xml) 异步的javascript 和 xml 是7种技术的综合,它包含了七个技术( javascript x ...
- 采用PHP实现”服务器推”技术的聊天室
传统的B/S结构的应用程序,都是采用”客户端拉”结束来实现客户端和服务器端的数据交换. 本文将通过结合Ticks(可以参看我的另外一篇文章:关于PHP你可能不知道的-PHP的事件驱动化设计),来实 ...
- Ajax与Gson
1. ajax的底层操作方法: 代码get请求 Get请求 2. Post请求 $("#chufa").click(function () { var val = $(" ...
- Java Netty 4.x 用户指南
问题 今天,我们使用通用的应用程序或者类库来实现互相通讯,比如,我们经常使用一个 HTTP 客户端库来从 web 服务器上获取信息,或者通过 web 服务来执行一个远程的调用. 然而,有时候一个通用的 ...
- 关于netty
现在我们一般使用应用程序或者链接库相互进行通信.例如,我们经常通过一个使用http协议的客户端链接库,从网站服务器中获得信息,并且通过网站服务,调用一些远程程序. 然而,一些通用协议或者它们的实现方式 ...
随机推荐
- CriminalIntent程序中Fragment相关内容
Activity中托管UI fragment有两种方式: 添加fragment到acitivity中 在activity代码中添加fragment 第一种方法即将fragment添加到acitivit ...
- 上门洗车App 竟然是块大肥肉!
http://www.leiphone.com/k-xiche-app-idea.html 打车App.租车App.防违规App我们见得多,但洗车App你一定没听过,之前在一次创业路演上碰到一个做上门 ...
- PowerShell管理Exchange
#添加Exchange管理单元get-pssnapin -registeredadd-pssnapin microsoft.exchange* #启用邮箱账号(需要域管理员权限,因为需要将某些属性写入 ...
- linux下mysql开启慢查询
mysql中最影响速度的就是那些查询很慢的语句.这些慢的语句,可能是写的不够合理或者是大数据下多表的联合查询等等.所以我们要找出这些语句,分析原因,加以优化. 1.方法1:用命令开启慢查询 1).查看 ...
- java 正则表达式学习
一. Java正则表达式 在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂. 因此,学习及使用正则表达式,便成了解决这一矛盾的主要手段. 正则表达式是一种可 ...
- jquery,extjs中的extend用法小结
在jquery中,extend其实在做插件时还是用的比较多的,今天同时小结jquery和ext js中 的extend用法,先来看jquery中的. 1) extend(dest,src1,src2 ...
- HttpResponse类
HttpReponse是服务器接收到浏览器的请求后,处理返回结果常用的一个类. 一.属性 Buffer 获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它. BufferOutput ...
- HDU 5590 ZYB's Biology 水题
ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- [AngularJS] Best Practise - Module
Module definitions Angular modules can be declared in various ways, either stored in a variable or u ...
- JDBC连接MySQL数据库及演示样例
JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...