PHP + jquery + ajax implement real-time chat function instance, jqueryajax

Source: Internet
Author: User

PHP + jquery + ajax implement real-time chat function instance, jqueryajax

This article describes how to implement the instant chat function using PHP + jquery + ajax. Share it with you for your reference. The details are as follows:

This is a simple source code for a chat room using jquery and php. Here we regularly use ajax to read the database and refresh it. The source code is directly referenced below. The instance code is as follows:

The index.html page is as follows:
Copy codeThe Code is as follows: <! 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> 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: 'first ',
Type: 'l'
}, Function (data ){
Chat. btn_status. _ true ();
Certificate ('domainmwebtime'0000.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: 'with ',
Type: 'l'
}, Function (data ){
Certificate ('domainmwebtime'0000.html (data. time );
$ ('# Chat textarea'). val (data. chat );
$ ('# Chat textarea'). stop (true, true). animate ({scrollTop: 9999}, 1 );
Chat. socket ();
});
},
Btn_status :{
_ False: function (){
Certificate ('wait chat_btn'wait .html ('waiting'). attr ('Disabled ', true );
},
_ True: function (){
Certificate ('audio chat_btn'audio .html ('speaker '). attr ('Disabled', false );
}
}
}
Chat. init ();
</Script>
</Head>

<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"> speech </button>
</Div>
<Div id = "mwebtime"> </div>
</Body>
</Html>

The data. php page is as follows:
Copy codeThe Code is as follows: <? Php
Header ("Expires: Mon, 26 Jul 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 = paitype.'.txt ';
If (isset ($ get) & isset ($ type) & file_exists ($ file )){
Switch ($ get ){
Case 'first ':
$ Chat = file_get_contents ($ file );
$ Json = array (
'Status' => 200,
'Time' => gmdate ("s "),
'Chat' => $ chat,
);
Echo json_encode ($ json );
Break;
Case 'with ':
$ 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 codeThe Code is 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, "");
$ 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 php programming.

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.