The business process for this example should be explained first:
1, the front-end user input content, and the input of the content of the words in real-time statistics.
2, users submit data, jquery implementation through Ajax to the background to send data.
3, the backstage PHP receives the data which submits the form, and carries on the necessary safe filtration to the data.
4, Backstage PHP connection MySQL database, and the submission of the form data to the corresponding data table.
5, the background to return the results of successful data content, and through Ajax, the returned data content inserted into the front-end page.
Above 1, 2 steps in the previous article: jquery chapter has been explained, this article will complete the rest of the walk.
Effect Chart:
Data tables
first we have to prepare a datasheet, the table structure is as follows:
CREATE TABLE ' say ' (
' id ' int () NOT NULL auto_increment,
' userid ' int (one) not null default ' 0 ',
' content ' VA Rchar NOT NULL,
' addtime ' int (a) not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;
Note that in this example, the Time field: the type of addtime is set to int for subsequent time processing, and in many applications, such as the Discuz forum, the time type is converted to a digital type.
timeline processing functions and formatted output list functions:
The timeline processing function is to convert time into what we see as "5 minutes Ago", "Yesterday 10:21", and the code is as follows:
/* Time conversion function *
/function Trantime ($time) {
$rtime = date ("m-d h:i", $time);
$htime = Date ("H:i", $time);
$time = Time ()-$time;
if ($time <) {
$str = ' just ';
}
ElseIf ($time <) {
$min = floor ($time/60);
$str = $min. ' Minutes ago ';
}
ElseIf ($time < *) {
$h = floor ($time/(60*60));
$str = $h. ' Hours ago '. $htime;
}
ElseIf ($time < * * 3) {
$d = floor ($time/(60*60*24));
if ($d ==1)
$str = ' Yesterday '. $rtime;
else
$str = ' Day before yesterday '. $rtime;
}
else {
$str = $rtime;
}
return $str;
}
The formatted output function is the function that will get the user information and the release content and time to the front-end page in a certain format, the code is as follows:
function Formatsay ($say, $dt, $uid) {
$say =htmlspecialchars (stripslashes ($say));
Return '
<div class= ' saylist ' ><a href= ' # ' ></a>
<div class= "Saytxt" >
<p><strong ><a href= "#" >demo_ '. $uid. ' </a></strong> '.
Preg_replace ('/(?: http|https|ftp): \/\/(?: [a-z0-9][a-z0-9_-]* (?: \. [A-z0-9] [a-z0-9_-]*] +):
? ( \d+)? \/? [^\s\ ' \]+]/I ', ' <a href= ' rel= ' nofollow ' target= ' blank ' >$1</a> ', $say). '
</p><div class= "Date" > '. Trantime ($DT). ' </div>
</div>
<div class= "clear" ></div>
</div> ';
}
Put all two of these functions into the function.php, ready to be invoked at any time.
submit.php Process form data
in the previous article, we knew that jquery would post the data from the front-end and submit it to the backstage submit.php via Ajax. Then the submit is to complete the follow-up of all the work. Please look at the code:
Require_once (' connect.php '); Database connection File
require_once (' function.php ');//function call file
$txt =stripslashes ($_post[' saytxt '));//Get the submitted data
$txt =mysql_real_escape_string (Strip_tags ($txt), $link); Filters HTML tags and escapes special characters
if (Mb_strlen ($txt) <1 | | Mb_strlen ($txt) >140)
die ("0");
=time (); Gets the current time
$userid =rand (0,4);
Inserts data into the datasheet
$query =mysql_query ("INSERT into say (userid,content,addtime) VALUES (' $userid ', ' $txt ', ' $time ')");
if (Mysql_affected_rows ($link)!=1)
die ("0");
Echo Formatsay ($txt, $time, $userid); Call function Output result
Note that in this example, the user ID (userid) is randomly processed for demonstration purposes, and the actual application is to get the ID of the current user. In addition to the database connection file, you can write their own, in the download I provided the demo also has this file.
Finally to return to the front page index.php. Index.php mainly in addition to provide input to the portal, but also to undertake the return of background processing results, and to the database to display the existing data. The code is as follows:
<?php
define (' Include_check ', 1);
Require_once (' connect.php ');
Require_once (' function.php ');
$query =mysql_query ("SELECT * from say ORDER BY id DESC limit 0,10");
while ($row =mysql_fetch_array ($query)) {
$sayList. =formatsay ($row [content], $row [Addtime], $row [userid]);
>
<form id= "MyForm" action= "say.php" method= "POST" >
The above is the entire content of this article, I hope to help you learn.