Php+jquery+ajax realization of Instant chat function Instance _php skill

Source: Internet
Author: User

This article is an example of how Php+jquery+ajax implements instant chat. Share to everyone for your reference. Specifically as follows:

This is a simple use of jquery and PHP to do a chat room source code, we are here to use Ajax to read the database and to refresh, the following direct reference to the source, the example codes are as follows:

The Index.html page is as follows:

Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script src= "Js/jquery-1.9.1.min.js" ></script>
<script>
var chat = {
Init:function () {
Chat.first ();
$ (' #chat_btn '). Unbind (' click '). Click (function () {
Chat.send ();
});
$ (' #my_chat '). KeyUp (function () {
if (Event.keycode = = 13) {
Chat.send ();
}
});
},
First:function () {
$.getjson (' data.php ', {
Action: ' I ',
Type: ' L '
},function (data) {
Chat.btn_status._true ();
$ (' #mwebtime '). HTML (data.time);
$ (' #chat textarea '). Val (data.chat);
$ (' #chat textarea '). Stop (True,true). Animate ({scrolltop:9999}, 1);
Chat.socket ();
});
},
Send:function () {
Chat.btn_status._false ();
$.getjson (' send.php ', {
txt:$ (' #my_chat '). Val (),
Type: ' L '
},function (data) {
if (data.status==200) {
Chat.btn_status._false ();
$ (' #my_chat '). Val (');
settimeout (function () {
Chat.btn_status._true ();
},2000);
}
});
},
Socket:function () {
$.getjson (' data.php ', {
Action: ' While ',
Type: ' L '
},function (data) {
$ (' #mwebtime '). HTML (data.time);
$ (' #chat textarea '). Val (data.chat);
$ (' #chat textarea '). Stop (True,true). Animate ({scrolltop:9999}, 1);
Chat.socket ();
});
},
btn_status:{
_false:function () {
$ (' #chat_btn '). html (' Wait '). attr (' disabled ', true);
},
_true:function () {
$ (' #chat_btn '). html (' speak '). attr (' disabled ', false);
}
}
}
Chat.init ();
</script>

<body>
<div id= "Chat" >
<textarea wrap= "Physical" style= "line-height:20px;font-size:12px;height:100px;width:200px;" ></textarea>
<br/>
<input id= "My_chat" type= "text"/>
<button id= "chat_btn" disabled= "disabled" > Speaking </button>
</div>
<div id= "Mwebtime" ></div>
</body>

The data.php page is as follows:

Copy Code code as follows:
<?php
Header ("Expires:mon, June June 1997 05:00:00 GMT");
Header ("last-modified:". Gmdate ("D, D M Y h:i:s"). " GMT ");
Header ("Cache-control:no-cache, must-revalidate");
Header ("Pramga:no-cache");
Set_time_limit (0);
$get = $_get[' action '];
$type = $_get[' type '];
$file = $type. TXT ';
if (Isset ($get) && isset ($type) && file_exists ($file)) {
Switch ($get) {
Case ' a ':
$chat = file_get_contents ($file);
$json =array (
' Status ' => 200,
' Time ' => gmdate ("s"),
' Chat ' => $chat,
);
echo Json_encode ($json);
Break
Case ' while ':
$oldsize = FileSize ($file);
$newsize = FileSize ($file);
while (true) {
if ($oldsize!= $newsize) {
$chat = file_get_contents ($file);
$json =array (
' Status ' => 200,
' Time ' => gmdate ("s"),
' Chat ' => $chat,
);
echo Json_encode ($json);
Exit
}
Clearstatcache ();
$newsize = FileSize ($file);
Usleep (10000);
}
Break
}
}
?>

The send.php page is as follows:

Copy Code code as follows:
<?php
$json = Array ();
$txt = isset ($_get[' txt '])? $_get[' txt ']: ';
$type = isset ($_get[' type '])? $_get[' type ': ';
if ($txt!= ') {
$file = $type. ". TXT ";
if (file_exists ($file)) {
$fp = fopen ($file, "a");
$STR = "rn". ' Admin: '. $txt;
$str = $txt. " n "//linux;
Fwrite ($fp, $STR);
Fclose ($FP);
$json [' Status ']=200;
echo Json_encode ($json);
Exit
}
}
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.