用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; } ?>
原文:http://www.cnblogs.com/lzzhuany/p/4893343.html