Ajax No Refresh chat room here is the use of php+ Ajax to implement the chat room
Database structure sql:
CREATE TABLE ' message ' (
' Mid ' int not NULL auto_increment,
' msg ' varchar (255) Not NULL,
' User ' varchar not NULL,
' Time ' int (ten) is not NULL,
PRIMARY KEY (' mid '),
KEY ' user ' (' user ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=161;
CREATE TABLE ' Session ' (
' ID ' varchar not NULL,
' Time ' int (ten) is not NULL,
' TimeNow ' int (a) not NULL,
' Data ' text not NULL,
' IP ' varchar not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;
index.html file code.
<! DOCTYPE htm Public "-//w3c//dtd XHTML 1.0 strict//en" [url=http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd]http:// Www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd[/url] ">
<HEAD>
<TITLE> Chat </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<link href= "Css/global.css" rel= "stylesheet" type= "Text/css"/>
<script language= "JavaScript" src= "Js/jquery.js" ></SCRIPT>
</HEAD>
<BODY>
<div id= "Main" >
<div id= "Main_left" style= "float:none;margin:0px auto;" >
<div id= "Chat_m" style= "Width:500px;height:500px;border: #cccccc 1px solid;overflow:auto;" >Loading...</div>
<div id= "Main_l_from" >
<form method=post action= "javascript:void (0)" >
Nickname: <input type= "text" name= "user" id= "user" size= "8" value= "<?=$_session[" User ']?> "> Content:
<input type= "text" name= "message" id= "message" style= "width:250px;" > <input type= "Submit" Name= "sub" value= "spoke" id= "sub" >
</div></FORM>
</div>
<!--<div id= "Main_right" >
</div>-->
<script language= "JavaScript" >
<!--
$ ("#chat_m"). Load ("chat.php");
$ ("#sub"). Click (function () {
if ($ ("#user"). val () = = ') {
Alert ("Nickname cannot be empty!");
}else if ($ ("#message"). val () = = ') {
Alert ("Chat content cannot be empty!");
}else{
$.post ("Ajax.php?act=send", {user:$ ("#user"). Val (), msg:$ ("#message"). Val ()},function (data) {
if (data.msg===true) {
ShowMsg ();
$ ("#message"). Val ("");
}else{
alert (data.msg);
}
}, "JSON")
}
})
-->
</SCRIPT>
<HR size=1>
Chat page. chat.html
<?php
Require_once ("config.php");
Program design: ℃ frozen tomato qq:7279915 e-mail:web@ye55.com [Url=http://www.phpd.cn]www.phpd.cn[/url]
$fist = $db->getfirst ("Select ' Mid ' from ' message ' order BY ' mid ' desc");
$mid = $fist [' mid ']-40; Initialize the number of chat record bars
$mid = $mid <=0?0: $mid;
$mid = 0;
?>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<div id= "Contents" ></div>
<input type= "text" style= "Display:none" Name= "mid" id= "Hide_mid" value= "<?= $mid?>" >
<div id= "End" style= "Width:400px;display:none;clear:both;" ></div>
<script language= "JavaScript" >
<!--
Alert ($ ("#hide_mid"). Val ());
function ShowMsg () {
mids=$ ("#hide_mid"). Val ();
$.post ("Ajax.php?act=display",
{Mid:mids},
function (data) {
alert (Data.list[2].user);
$ ("#hide_mid"). Val (Data.mid);
if (data.list!=0) {
for (i=0;i<data.list.length;i++) {
$ ("#contents"). Append ("<br/>" +data.list[i].user+ "said:" +data.list[i].msg+ " ["+data.list[i].time1+"]);
ScrollWindow ();
}
}
}, "JSON");
}
function ScrollWindow () {Scroll (0, 100000);}
ShowMsg ();
SetInterval ("ShowMsg ()", 4000);
-->
</SCRIPT>
The last is the PHP page, which is used to process data published by the user.
<?php
Require_once ("config.php");
Program design: ℃ Frozen tomato qq:7279915
Send message
if ($_get[' act ']== ' send ') {
$msg =$_post[' msg '];
$user =$_post[' user '];
if (Empty ($msg) | | empty ($user)) {
$return [' msg ']= ' chat content or nickname can not be empty ';
}else{
$time =time ();
if ($db->insert (' msg ', ' user ', ' time ') VALUES (' $msg ', ' $user ', ' $time ')) {
$return [' msg ']=true;
}else{
$return [' msg ']= ' err ';
}
}
echo Json_encode ($return);
}elseif ($_get[' act ']== ' display ') {
if (Isset ($_post[' mid ')) {
$s = "where ' mid ' > '". $_post[' mid ']. "' ";
}else{
$s = ';
}
$sql = "SELECT * from ' message ' $s order by ' mid '";
$total = $db->getcount ($sql);
if ($total ==0) {
$a = 0;
}else{
$result = $db->query ($sql);
while ($row = $db->getarray ($result)) {
$row [' time1 ']=date (' y-m-d h:i:s ', $row [' time ']);
$a []= $row;
$b = $row [' mid '];
}
}
$CCC =array (' list ' => $a, ' mid ' => $b);
echo Json_encode ($CCC);
}
?>