PHP + MySQL + jQuery implement microblog program PHP

Source: Internet
Author: User
PHP + MySQL + jQuery implements the release of Weibo program jQuery, which explains how to use jQuery to implement real-time word count statistics for input forms and Ajax to submit data to the background. This article will introduce how the background processes the data submitted by the foreground and returns the results. First, describe the Business Process in this example: 1. The frontend user inputs the content and

PHP + MySQL + jQuery implements the release of Weibo program jQuery, which explains how to use jQuery to implement real-time word count statistics for input forms and Ajax to submit data to the background. This article will introduce how the background processes the data submitted by the foreground and returns the results. First, describe the Business Process in this example: 1. The frontend user inputs the content and

PHP + MySQL + jQueryImplementationReleaseWeiboProgram-- JQuery explains how to use jQueryImplementationInput Form Real-Time Word Count statistics and Ajax data submission to the background function. This article will introduce how the background processes the data submitted by the foreground and returns the results.

First, describe the business process of this example:

1. The frontend user inputs the content and counts the number of words in the input content in real time.

2. the user submits data, jQueryImplementationSend data to the background through Ajax.

3. the backend PHP receives the data in the submitted Form and performs necessary security filtering on the data.

4. the backend PHP connects to the Mysql database and writes the submitted form data to the corresponding data table.

5. The background will return the successful result data content, and insert the returned data content to the front-end page through Ajax.

Steps 1 and 2 are described in the previous article: jQuery, which completes the rest of this article.

Prepare a data table. The table structure is as follows:

1. CREATETABLE `say` (
2. `id` int(11) NOTNULL auto_increment,
3. `userid` int(11) NOTNULLdefault'0',
4. `content` varchar(200) NOTNULL,
5. `addtime` int(10) NOTNULL,
6. PRIMARYKEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Note: In this example, the time field 'addtime' type is set to int' to facilitate subsequent time processing. In many applications (such as the Discuz Forum), the time field is converted to the numeric type.

Timeline processing function and formatted output list function: the timeline processing function converts the time into the formats we see, such as "5 minutes ago" and "yesterday, for more information, see my previous article: PHPImplementationThe timeline Function Code is as follows:

1./* time Conversion Function */
2. function tranTime ($ time ){
3. $ rtime = date ("m-d H: I", $ time );
4. $ htime = date ("H: I", $ time );
5. $ timetime = time ()-$ time;
6.
7. if ($ time <60 ){
8. $ str = 'hangzhou ';
9 .}
10. elseif ($ time <60*60 ){
11. $ min = floor ($ time/60 );
12. $ str = $ min. 'minute ago ';
13 .}
14. elseif ($ time <60*60*24 ){
15. $ h = floor ($ time/(60*60 ));
16. $ str = $ h. 'hour before '. $ htime;
17 .}
18. elseif ($ time <60*60*24*3 ){
19. $ d = floor ($ time/(60*60*24 ));
20. if ($ d = 1)
21. $ str = 'Yesterday '. $ rtime;
22. else
23. $ str = 'day before Day'. $ rtime;
24 .}
25. else {
26. $ str = $ rtime;
27 .}
28. return $ str;
29 .}

The formatting output function returns the user information andReleaseThe function that outputs content and time to the front-end page in a certain format. The Code is as follows:

1.function formatSay($say,$dt,$uid){
2.$say=htmlspecialchars(stripslashes($say));
3.
4.return'
5.


6. '.$uid.'.jpg" width="50" height="50" alt="demo" />
7.
8.


9.

demo_'.$uid.' '.
10.preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+): ?(\d+)?\/?[^\s\"\']+)/i',
11. '$1',$say).'
12.


13.

'.tranTime($dt).'


14.


15.


16.

'
17. ; }

Put the above two functions into function. php and prepare to be called at any time.

Submit. php processes form data

In the previous article, we learned that jQuery submitted the data obtained by the front-end to submit. php In the background using the POST method. Then, submit is to complete all subsequent tasks. See the Code:

1. require_once ('connect. php'); // database connection file
2. require_once ('function. php'); // function call File
3.
4. $ txt = stripslashes ($ _ POST ['saytxt ']); // obtain submitted data
5. $ txt = mysql_real_escape_string (strip_tags ($ txt), $ link );//
6. if (mb_strlen ($ txt) <1 | mb_strlen ($ txt)> 140)
7. die ("0"); // determines whether the number of input characters meets the requirements
8.
9. $ timetime = time (); // get the current time
10. $ userid = rand (0, 4 );
11. // insert data to the data table
12. $ query = mysql_query ("insert into say (userid, content, addtime) values ('$ userid',' $ txt ',' $ Time ')");
13. if (mysql_affected_rows ($ link )! = 1)
14. die ("0 ");
15. echo formatSay ($ txt, $ time, $ userid); // call the function output result

Note: In this example, the user ID (userid) is randomly processed for demonstration. The actual application is to obtain the ID of the current user. In addition, you can write a database connection file by yourself. This file is also available in the downloaded DEMO provided by me.

Finally, return to index. php on the front-end page. Index. php not only provides the input entry, but also processes the returned results in the background and displays the existing data in the database. The Code is as follows:

1. 2. define ('include _ check', 1 );
3. require_once ('connect. php ');
4. require_once ('function. php ');
5.
6. $ query = mysql_query ("select * from say order by id desc limit 0, 10 ");
7. while ($ row = mysql_fetch_array ($ query )){
8. $ sayList. = formatSay ($ row [content], $ row [addtime], $ row [userid]);
9 .}
10.?>
11.


12.

140Tell me what you are doing...


13.
14.


15.
16.
17.


18.
19.


20.


21.
22.

So far, this example is from frontend interaction to background processing.ProgramAll done. If you are interested, you 'd better do it yourself to have fun.

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.