Code example of the client communication function implemented by ThinkPHP combined with ajax and Mysql _ PHP Tutorial

Source: Internet
Author: User
Code example of the client communication function implemented by ThinkPHP combined with ajax and Mysql. This instance uses ThinkPHP in combination with Ajax and mysql to implement the client communication function, as follows: 1. use the ajax partial refresh function of js, each refresh inserts the new record read from the database into this instance. using ThinkPHP combined with Ajax and mysql, the client communication function is implemented as follows:

1. use the ajax partial refresh function of js to insert the new record read from the database to the display area of the page. the code is as follows:

Window. onload = setInterval (showWords1000); // refresh after loading

Function showWords () // call the function during refresh to implement the ajax request {xmlHttp = GetXmlHttpObject (); // Obtain the request object from the custom function. If (xmlHttp = null) {alert ("Browser does not support HTTP Request") return;} var url = "index. php? G = Ksks & m = Comments & a = refresh "; // The requested path uses the shuipfcms framework. Therefore, g is a group and m is an action controller, a is the template url = url + "& data_id =" + $ ("# data_id "). val (); // The parameter passed by data_id, used to query the database id url = url + "& lasttime =" + $ ("I "). last (). siblings ('input '). val (); // lasttime is the timestamp of the last record of the data displayed on the page. xmlHttp is searched based on conditions greater than the timestamp. onreadystatechange = stateChanged; // The xmlHttp function called in response to the status change. open ("GET", url, true); // use the get method to submit xmlHttp. send (); // after passing the preceding parameter, can this sentence be required?} function stateChanged () // implement the function of inserting data into the page {var str = ""; if (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete") {eval ("var arrs =" + xmlHttp. responseText); // obtain the data output by the action Response. because of the php array format in the data room, you need to convert eval or json to the js array format var chp =$ ("# conversation_history "); // communication chat record box for (var j = 0; j"); // Define the html code of the record box in the page var I = $ (""+ Arrs [j]. times +""); // I tag I is defined by thinkphp to directly output the time to the input to hide and store the chat sending time in the data var p = $ ("

"); P.html (arrs [j]. content); // chat content p. append (I); var h3value = arrs [j]. name; // The sender's obj. find ("h3" ).html (h3value + ":"); if (h3value = "host") {obj. find ("h3 "). addClass ("presenter");} // The host displays obj. find ("td: last "). append (p); chp. append (obj); // append it to the table} chp. parent (). scrollTop (chp. parent () [0]. scrollHeight + 0); // scroll to the bottom }}

Function GetXmlHttpObject () // defines the ajax request object {var xmlHttp = null; try {// Firefox, Opera 8.0 +, Safari xmlHttp = new XMLHttpRequest ();} catch (e) {// Internet Explorer try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}} return xmlHttp ;}

2. the code of the action Controller is as follows:

Function refresh () {$ data_id = I ('Get. data_id '); // Obtain the Guest id $ lasttime = I ('Get. lasttime '); // print_r (date ("Y-m-d H: I: s", $ lasttime); if (! $ Data_id) $ this-> error ("this interview does not exist! "); $ Data = M (" ksks_words_memoir "); // print_r (date ('Y-m-d H: I: S', $ lasttime); if (! $ Data) $ this-> error ("initialization data failed! "); // $ Words = $ data-> where (array ('dataid' => $ data_id)-> order ('Time asc ')-> select (); // print_r ($ data-> where (array ('time' => $ lasttime)-> select ()); $ where ['Time'] = array ('GT ', $ lasttime); $ count = $ data-> where ($ where)-> count (); // print_r (date ("Y-m-d H: I: s", $ lasttime); $ words = $ data-> where ($ where) -> select (); $ datas = array (); // array to be returned $ I = 0; foreach ($ words as $ word) {$ datas [$ I] ['name'] = $ word ['name']; $ datas [$ I] ['content'] = $ word ['content']; $ datas [$ I] ['Times '] = date ('Y-m-d H: I: S', $ word ['Time']); $ lasttime = $ word ['Time']; $ I = $ I + 1;} setCookie ('lasttime', $ lasttime); $ datas = json_encode ($ datas ); // $ this-> cache (); echo $ datas ;}

3. the code on the chat page is as follows:

 
  class="presenter">{$vo.name}:
  

{$vo.content} {$vo.time|date="Y-m-d H:i:s",###}

Tip 1. use the ajax partial refresh function of js to insert the new record read from the database every time...

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.