Thinkphp uses ajax, jquery, and Mysql to implement the simple client communication function AjaxThinkPHP
1. use the ajax partial refresh function of js to insert the new records read from the database to the display area of the page each refresh and directly run the code:
Window. onload = setInterval (showWords1000); // refresh after loading
Function showWords () // call the function when refreshing to implement ajax requests
{
XmlHttp = GetXmlHttpObject (); // Obtain the request object from a 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, m is an action controller, and a is a template.
Url = url + "& data_id =" + $ ("# data_id"). val (); // connect to 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. onreadystatechange = stateChanged; // The function called in response to a status change
XmlHttp. open ("GET", url, true); // use the get method to submit
XmlHttp. send (); // after passing the parameter above, this sentence is optional
}
Function stateChanged () // inserts 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 the php array format in the data room needs to be converted to the js array format using eval or json.
Var chp = $ ("# conversation_history"); // communication chat record box
For (var j = 0; j var obj = $ (""); // Define the html format code of the record box on the page
Var I = $ (""+ Arrs [j]. times +""); // I tag I is defined by thinkphp to directly output time
// Hide input 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; // sender
Obj. find ("h3" ).html (h3value + ":");
If (h3value = "host ")
{Obj. find ("h3"). addClass ("presenter");} // The host will display
Obj. find ("td: last"). append (p );
Chp. append (obj); // append to 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 action Controller directly goes to the code:
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;
}
// Check the competition. do not explain it too much and talk about it later.
3. enter the code on the chat page:
Class = "presenter">{$ Vo. name }:
{$ Vo. content}{$ Vo. time | date = "Y-m-d H: I: s ",###}
// Watch the World Cup and talk about it later