Php+mysql+jquery implementation of--php _php tutorial to release Weibo program

Source: Internet
Author: User
In the previous article, "Php+mysql+jquery implementation of the microblogging program--jquery," explains how to use jquery to achieve the real-time word count of input forms and the function of Ajax to submit data to the background. This article will show you how the background handles the data submitted by the foreground and returns the results.

Let's start by explaining the business process for this example:

1, the front-end user input content, and the input of the content of the words in real-time statistics.

2, the user submits the data, jquery realizes through the Ajax sends the data in the background.

3, the background PHP receives the data submitted form, and the data for the necessary security filtering.

4. Background PHP connects to MySQL database and writes the submitted form data to the corresponding data table.

5, backstage to return the successful result data content, and through Ajax to insert the returned data content into the front page.

The above 1, 2 steps in the previous article: The jquery chapter has been explained, this article will complete the rest of the section.

Data table First we want to prepare a data table, the table structure is as follows:

 
  
  
  1. CREATE TABLE ' say ' (
  2. ' id ' int (one) not NULL auto_increment,
  3. ' userid ' int (one) not NULL default ' 0 ',
  4. ' Content ' varchar (+) not NULL,
  5. ' Addtime ' int (ten) is not NULL,
  6. PRIMARY KEY ENGINE=MyISAMCHARSET =UTF8

Note that in this example, the Time field: the type of addtime is set to int, it is convenient 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 handler function, which is to convert the time to what we see like "5 minutes Ago", "Yesterday 10:21", and more, we can look at the article I wrote earlier: PHP implements the timeline function, the 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. $ Time Time = Time ()-$time;
  6. if ($time < ) {
  7. $ Str = ' Just ' ;
  8. }
  9. else if ($time < ) {
  10. $ min = Floor ($time/60);
  11. $ Str = $min. ' Minutes ago ';
  12. }
  13. else if ($time < *) {
  14. $ h = Floor ($time/(60*60));
  15. $ Str = $h. ' Hour ago '. $htime;
  16. }
  17. else if ($time < * 3) {
  18. $ D = Floor ($time/(60*60*24));
  19. if ($D==1)
  20. $ Str = ' Yesterday ' . $rtime;
  21. Else
  22. $ Str = ' The day before yesterday ' . $rtime;
  23. }
  24. else {
  25. $ Str = $rtime;
  26. }
  27. return $str;
  28. }

The formatted output function is the function to output the user information and the published content and time in a certain format to the front page, the code is as follows:

 
 
  1. function Formatsay ($say, $dt, $uid) {
  2. $ say = Htmlspecialchars (Stripslashes ($say));
  3. Return
  4. < div class = "saylist"
  5. < a href="#"><img src = "images/". $uid. JPG " width=" " height=" " alt=" Demo " />
  6. a >
  7. < Div class="saytxt">
  8. < P > < Strong > < a href="#">demo_ '. $uid. ' a> strong> '.
  9. Preg_replace ('/(?: http|https|ftp): \/\/(?: [a-z0-9][a-z0-9_-]* (?: \. [A-z0-9] [a-z0-9_-]*) +):? (\d+)? \/? [^\s\ "\ ']+)/I ',
  10. ' < a href="$" rel="nofollow" Target = "Blank" > $ a>', $say). '
  11. P >
  12. < Div class="date">'. Trantime ($DT). ' div >
  13. Div >
  14. < Div class="clear"> div>
  15. Div > '
  16. ; }

Put all two of these functions into function.php, ready to be called at any time.

submit.php processing form data

In the previous article, we know that jquery submits the data from the front end to the submit.php in the background via Ajax. Then submit is to complete all the following tasks. Please look at the code:

 
 
  1. Require_once (' connect.php '); Database Connection File
  2. Require_once (' function.php '); Function call File
  3. $ txt = stripslashes ($_post[' saytxt ');//Get Submitted data
  4. $ txt = mysql_real_escape_string (Strip_tags ($txt), $link);//filter HTML tags and escape special characters
  5. if (Mb_strlen ($txt)<1 | | mb_strlen ($txt)>
  6. Die ("0"); Determine if the number of input characters meets the requirements
  7. $ Time Time =time ();//Get Current time
  8. $ userid = Rand (0,4);
  9. Inserting data into a data table
  10. $ Query = mysql_query ("INSERT into say (userid,content,addtime) VALUES (' $userid ', ' $txt ', ' $time ')");
  11. if (Mysql_affected_rows ($link)!=1)
  12. Die ("0");
  13. Echo Formatsay ($txt, $time, $userid); Call function Output result

Note that in this case, 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 files, you can write their own, in the download I provided the demo also has this file.

Finally, go back to the front page index.php. Index.php mainly in addition to provide input entry, but also to take the background processing the returned results, and to the data in the database to display. The code is as follows:

 
 
  1. Php
  2. Define (' Include_check ', 1);
  3. Require_once (' connect.php ');
  4. Require_once (' function.php ');
  5. $ Query = mysql_query ("SELECT * from say ORDER BY id DESC limit 0,10");
  6. While ($row=mysql_fetch_array($query)) {
  7. $ saylist. = Formatsay ($row [content], $row [Addtime], $row [UserID];
  8. }
  9. ?>
  10. < form id = "MyForm" action = "say.php" method = "POST" span class= "tag" >>
  11. < h3 > span class= "tag" >< span class = "counter" > !-- span > tell me what you are doing ... !-- h3 >
  12. < textarea span class= "attribute" >name = "saytxt" id = "saytxt" class = "input" tabindex = "1" rows = "2" cols = "Max" > !-- textarea >
  13. < P >
  14. < input type="Submit" class="SUB_BTN" value = "commit" disabled = " Disabled " />
  15. < span id="msg"> span >
  16. P >
  17. form >
  18. < Div class="clear"> div >
  19. < Div id="saywrap">
  20. PHP echo $sayList; ?>
  21. Div >

At this point, this example from the front-end interaction to the background processing program, all completed, interested students have better hands-on, to learn from the fun.

http://www.bkjia.com/PHPjc/587917.html www.bkjia.com true http://www.bkjia.com/PHPjc/587917.html techarticle In the previous article, "Php+mysql+jquery implementation of the microblogging program jquery," explains how to use jquery to implement the input form of real-time word count and Ajax to submit data to the background function. This article ...

  • Related Article

    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.