PHP+MySQL+jQuery实现发布微博程序PHP篇
PHP+MySQL+jQuery 实现 发布 微博 程序 jQuery篇》中,讲解了如何用jQuery 实现 输入表单的实时字数统计和Ajax提交数据到后台的功能。本文将重要介绍后台是如何处理前台提交的数据,并返回结果的。 先还是要说明本例的业务流程: 1、前端用户输入内容,并对
PHP+MySQL+jQuery实现发布微博程序——jQuery篇》中,讲解了如何用jQuery实现输入表单的实时字数统计和Ajax提交数据到后台的功能。本文将重要介绍后台是如何处理前台提交的数据,并返回结果的。
先还是要说明本例的业务流程:
1、前端用户输入内容,并对输入的内容字数进行实时统计。
2、用户提交数据,jQuery实现通过Ajax向后台发送数据。
3、后台PHP接收提交表单的数据,并对数据进行必要的安全过滤。
4、后台PHP连接Mysql数据库,并将提交过来的表单数据写入到相应的数据表中。
5、后台向返回成功结果数据内容,并通过Ajax将返回的数据内容插入到前端页面中。
上述1、2步在前篇文章:jQuery篇已讲解了,本文将完成剩余的部分。
数据表首先我们要准备一个数据表,表结构如下:
<p><span>1</span><span>. </span><span>CREATE</span><span>TABLE</span><span> `say` ( <br> </span><span>2</span><span>. `id` </span><span>int</span><span>(</span><span>11</span><span>) </span><span>NOT</span><span>NULL</span><span> auto_increment, <br> </span><span>3</span><span>. `userid` </span><span>int</span><span>(</span><span>11</span><span>) </span><span>NOT</span><span>NULL</span><span>default</span><span>'</span><span>0</span><span>'</span><span>, <br> </span><span>4</span><span>. `content` </span><span>varchar</span><span>(</span><span>200</span><span>) </span><span>NOT</span><span>NULL</span><span>, <br> </span><span>5</span><span>. `addtime` </span><span>int</span><span>(</span><span>10</span><span>) </span><span>NOT</span><span>NULL</span><span>, <br> </span><span>6</span><span>. </span><span>PRIMARY</span><span>KEY</span><span> (`id`) ) ENGINE</span><span>=</span><span>MyISAM </span><span>DEFAULT</span><span> CHARSET</span><span>=</span><span>utf8; </span></p>
注意,本例中将时间字段:addtime的类型设置为int,是为了后续的时间处理方便,在很多应用中(如Discuz论坛)都是将时间类型转成数字型。
时间轴处理函数和格式化输出列表函数:时间轴处理函数,就是把时间转换成我们看到的诸如“5分钟前”,“昨天 10:21”等形式,详细使用可以查看之前我写过的文章:PHP实现时间轴函数,代码如下:
<p><span>1</span><span>.</span><span>/*</span><span>时间转换函数</span><span>*/</span><span> <br> </span><span>2</span><span>.</span><span>function</span><span> tranTime(</span><span>$time</span><span>) { <br> </span><span>3</span><span>.</span><span>$rtime</span><span>=</span><span>date</span><span>(</span><span>"</span><span>m-d H:i</span><span>"</span><span>,</span><span>$time</span><span>); <br> </span><span>4</span><span>.</span><span>$htime</span><span>=</span><span>date</span><span>(</span><span>"</span><span>H:i</span><span>"</span><span>,</span><span>$time</span><span>); <br> </span><span>5</span><span>.</span><span>$timetime</span><span>=</span><span>time</span><span>() </span><span>-</span><span>$time</span><span>; <br> </span><span>6</span><span>.</span><span> <br> </span><span>7</span><span>.</span><span>if</span><span> (</span><span>$time</span><span><span>60</span><span>) { <br> </span><span>8</span><span>.</span><span>$str</span><span>=</span><span>'</span><span>刚刚</span><span>'</span><span>; <br> </span><span>9</span><span>.</span><span> } <br> </span><span>10</span><span>.</span><span>else</span><span>if</span><span> (</span><span>$time</span><span><span>60</span><span>*</span><span>60</span><span>) { <br> </span><span>11</span><span>.</span><span>$min</span><span>=</span><span>floor</span><span>(</span><span>$time</span><span>/</span><span>60</span><span>); <br> </span><span>12</span><span>.</span><span>$str</span><span>=</span><span>$min</span><span>.</span><span>'</span><span>分钟前</span><span>'</span><span>; <br> </span><span>13</span><span>.</span><span> } <br> </span><span>14</span><span>.</span><span>else</span><span>if</span><span> (</span><span>$time</span><span><span>60</span><span>*</span><span>60</span><span>*</span><span>24</span><span>) { <br> </span><span>15</span><span>.</span><span>$h</span><span>=</span><span>floor</span><span>(</span><span>$time</span><span>/</span><span>(</span><span>60</span><span>*</span><span>60</span><span>)); <br> </span><span>16</span><span>.</span><span>$str</span><span>=</span><span>$h</span><span>.</span><span>'</span><span>小时前 </span><span>'</span><span>.</span><span>$htime</span><span>; <br> </span><span>17</span><span>.</span><span> } <br> </span><span>18</span><span>.</span><span>else</span><span>if</span><span> (</span><span>$time</span><span><span>60</span><span>*</span><span>60</span><span>*</span><span>24</span><span>*</span><span>3</span><span>) { <br> </span><span>19</span><span>.</span><span>$d</span><span>=</span><span>floor</span><span>(</span><span>$time</span><span>/</span><span>(</span><span>60</span><span>*</span><span>60</span><span>*</span><span>24</span><span>)); <br> </span><span>20</span><span>.</span><span>if</span><span>(</span><span>$d</span><span>==</span><span>1</span><span>) <br> </span><span>21</span><span>.</span><span>$str</span><span>=</span><span>'</span><span>昨天 </span><span>'</span><span>.</span><span>$rtime</span><span>; <br> </span><span>22</span><span>.</span><span>else</span><span> <br> </span><span>23</span><span>.</span><span>$str</span><span>=</span><span>'</span><span>前天 </span><span>'</span><span>.</span><span>$rtime</span><span>; <br> </span><span>24</span><span>.</span><span> } <br> </span><span>25</span><span>.</span><span>else</span><span> { <br> </span><span>26</span><span>.</span><span>$str</span><span>=</span><span>$rtime</span><span>; <br> </span><span>27</span><span>.</span><span> } <br> </span><span>28</span><span>.</span><span>return</span><span>$str</span><span>; <br> </span><span>29</span><span>.</span><span> } </span></span></span></span></span></p>
格式化输出函数是将得到的用户信息和发布内容及时间按照一定的格式输出到前端页面的函数,代码如下:
<p><span>1</span><span>.</span><span>function</span><span> formatSay(</span><span>$say</span><span>,</span><span>$dt</span><span>,</span><span>$uid</span><span>){ <br> </span><span>2</span><span>.</span><span>$say</span><span>=</span><span>htmlspecialchars</span><span>(</span><span>stripslashes</span><span>(</span><span>$say</span><span>)); <br> </span><span>3</span><span>.</span><span> <br> </span><span>4</span><span>.</span><span>return</span><span>'</span><span> <br> 5. <div class="saylist"> <br> 6. <a href="#"><img src="/static/imghw/default1.png" data-src="images/</span><span>'</span><span>.</span><span>$uid</span><span>.</span><span>'</span><span>.jpg" class="lazy" alt="PHP+MySQL+jQuery实现发布微博程序PHP篇" ><span>.</span><span>$uid</span><span>.</span><span>'</span><span>.jpg" width="50" style="max-width:90%" alt="demo" /> <br> 7. </span></a> <br> 8. <div class="saytxt"> <br> 9. <p><strong><a href="#">demo_<span>'</span><span>.</span><span>$uid</span><span>.</span><span>'</span><span></span></a></strong> <span>'</span><span>.</span><span> <br> </span><span>10</span><span>.</span><span>preg_replace</span><span>(</span><span>'</span><span>/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+): ?(\d+)?\/?[^\s\"\</span><span>'</span><span>]</span><span>+</span><span>)</span><span>/</span><span>i</span><span>'</span><span>, <br> 11. </span><span>'</span><span><span>a href</span><span>=</span><span>"</span><span>$1</span><span>"</span><span> rel</span><span>=</span><span>"</span><span>nofollow</span><span>"</span><span> target</span><span>=</span><span>"</span><span>blank</span><span>"</span><span>></span><span>$</span><span>1</span><span></span><span>a</span><span>></span><span>'</span><span>,$say).</span><span>'</span><span> <br> </span><span>12</span><span>.</span><span></span><span>p</span><span>></span><span> <br> </span><span>13</span><span>.</span><span><span>div </span><span>class</span><span>=</span><span>"</span><span>date</span><span>"</span><span>></span><span>'</span><span>.tranTime($dt).</span><span>'</span><span></span><span>div</span><span>></span><span> <br> </span><span>14</span><span>.</span><span></span><span>div</span><span>></span><span> <br> </span><span>15</span><span>.</span><span><span>div </span><span>class</span><span>=</span><span>"</span><span>clear</span><span>"</span><span>></span><span>div</span><span>></span><span> <br> </span><span>16</span><span>.</span><span></span><span>div</span><span>></span><span>'</span><span> <br> 17. ; } </span></span></span></span></p> <p> 将以上两个函数都放入function.php中,准备随时被调用。</p> <p><strong> submit.php处理表单数据</strong></p> <p> 在上篇文章中,我们知道jQuery将前端获得的数据以POST方式,通过Ajax提交给了后台的submit.php。那么submit就是要完成后续的所有一摊子任务。请看代码:</p> <p> </p> <pre class="brush:php;toolbar:false"><p><span>1</span><span>.</span><span>require_once</span><span>(</span><span>'</span><span>connect.php</span><span>'</span><span>); </span><span>//</span><span>数据库连接文件 </span><span><br></span><span>2</span><span>.</span><span>require_once</span><span>(</span><span>'</span><span>function.php</span><span>'</span><span>); </span><span>//</span><span>函数调用文件 </span><span><br></span><span>3</span><span>.</span><span> <br> </span><span>4</span><span>.</span><span>$txt</span><span>=</span><span>stripslashes</span><span>(</span><span>$_POST</span><span>[</span><span>'</span><span>saytxt</span><span>'</span><span>]); </span><span>//</span><span>获取提交的数据 </span><span><br></span><span>5</span><span>.</span><span>$txt</span><span>=</span><span>mysql_real_escape_string</span><span>(</span><span>strip_tags</span><span>(</span><span>$txt</span><span>)</span><span>,</span><span>$link</span><span>); </span><span>//</span><span><br></span><span>6</span><span>.</span><span>if</span><span>(mb_strlen(</span><span>$txt</span><span>)</span><span><span>1</span><span>||</span><span> mb_strlen(</span><span>$txt</span><span>)</span><span>></span><span>140</span><span>) <br> </span><span>7</span><span>.</span><span>die</span><span>(</span><span>"</span><span>0</span><span>"</span><span>); </span><span>//</span><span>判断输入字符数是否符合要求 </span><span><br></span><span>8</span><span>.</span><span> <br> </span><span>9</span><span>.</span><span>$timetime</span><span>=</span><span>time</span><span>(); </span><span>//</span><span>获取当前时间 </span><span><br></span><span>10</span><span>.</span><span>$userid</span><span>=</span><span>rand</span><span>(</span><span>0</span><span>,</span><span>4</span><span>); <br> </span><span>11</span><span>.</span><span>//</span><span>插入数据到数据表中 </span><span><br></span><span>12</span><span>.</span><span>$query</span><span>=</span><span>mysql_query</span><span>(</span><span>"</span><span>insert into say(userid,content,addtime)values('</span><span>$userid</span><span>','</span><span>$txt</span><span>','</span><span>$time</span><span>')</span><span>"</span><span>); <br> </span><span>13</span><span>.</span><span>if</span><span>(</span><span>mysql_affected_rows</span><span>(</span><span>$link</span><span>)</span><span>!=</span><span>1</span><span>) <br> </span><span>14</span><span>.</span><span>die</span><span>(</span><span>"</span><span>0</span><span>"</span><span>); <br> </span><span>15</span><span>.</span><span>echo</span><span> formatSay(</span><span>$txt</span><span>,</span><span>$time</span><span>,</span><span>$userid</span><span>); </span><span>//</span><span>调用函数输出结果 </span></span></p>
注意,本例中为了演示,将用户ID(userid)进行随机处理,实际的应用是获取当前用户的ID。另外数据库连接文件,大家可以自己写一个,在我提供的下载的DEMO里也有这个文件。
最后要回到前端页面index.php来。index.php主要除了提供输入的入口,还要承接后台处理返回的结果,并且要将数据库里已有的数据显示出来。代码如下:
<p><span>1</span><span>.</span><span></span><span>php <br> </span><span>2</span><span>.</span><span>define</span><span>(</span><span>'</span><span>INCLUDE_CHECK</span><span>'</span><span>,</span><span>1</span><span>); <br> </span><span>3</span><span>.</span><span>require_once</span><span>(</span><span>'</span><span>connect.php</span><span>'</span><span>); <br> </span><span>4</span><span>.</span><span>require_once</span><span>(</span><span>'</span><span>function.php</span><span>'</span><span>); <br> </span><span>5</span><span>.</span><span> <br> </span><span>6</span><span>.</span><span>$query</span><span>=</span><span>mysql_query</span><span>(</span><span>"</span><span>select * from say order by id desc limit 0,10</span><span>"</span><span>); <br> </span><span>7</span><span>.</span><span>while</span><span> (</span><span>$row</span><span>=</span><span>mysql_fetch_array</span><span>(</span><span>$query</span><span>)) { <br> </span><span>8</span><span>.</span><span>$sayList</span><span>.=</span><span>formatSay(</span><span>$row</span><span>[content]</span><span>,</span><span>$row</span><span>[addtime]</span><span>,</span><span>$row</span><span>[userid]); <br> </span><span>9</span><span>.</span><span> } <br> </span><span>10</span><span>.</span><span>?></span><span> <br> </span><span>11</span><span>.</span><span><span>form id</span><span>=</span><span>"</span><span>myform</span><span>"</span><span> action</span><span>=</span><span>"</span><span>say.php</span><span>"</span><span> method</span><span>=</span><span>"</span><span>post</span><span>"</span><span>></span><span> <br> </span><span>12</span><span>.</span><span><span>h3</span><span>><span>span </span><span>class</span><span>=</span><span>"</span><span>counter</span><span>"</span><span>></span><span>140</span><span></span><span>span</span><span>></span><span>说说你正在做什么</span><span>...</span><span>h3</span><span>></span><span> <br> </span><span>13</span><span>.</span><span><span>textarea name</span><span>=</span><span>"</span><span>saytxt</span><span>"</span><span> id</span><span>=</span><span>"</span><span>saytxt</span><span>"</span><span>class</span><span>=</span><span>"</span><span>input</span><span>"</span><span> tabindex</span><span>=</span><span>"</span><span>1</span><span>"</span><span> rows</span><span>=</span><span>"</span><span>2</span><span>"</span><span> cols</span><span>=</span><span>"</span><span>40</span><span>"</span><span>></span><span>textarea</span><span>></span><span> <br> </span><span>14</span><span>.</span><span><span>p</span><span>></span><span> <br> </span><span>15</span><span>.</span><span><span>input type</span><span>=</span><span>"</span><span>submit</span><span>"</span><span>class</span><span>=</span><span>"</span><span>sub_btn</span><span>"</span><span> value</span><span>=</span><span>"</span><span>提 交</span><span>"</span><span> disabled</span><span>=</span><span>"</span><span>disabled</span><span>"</span><span>/></span><span> <br> </span><span>16</span><span>.</span><span><span>span id</span><span>=</span><span>"</span><span>msg</span><span>"</span><span>></span><span>span</span><span>></span><span> <br> </span><span>17</span><span>.</span><span></span><span>p</span><span>></span><span> <br> </span><span>18</span><span>.</span><span></span><span>form</span><span>></span><span> <br> </span><span>19</span><span>.</span><span><span>div </span><span>class</span><span>=</span><span>"</span><span>clear</span><span>"</span><span>></span><span>div</span><span>></span><span> <br> </span><span>20</span><span>.</span><span><span>div id</span><span>=</span><span>"</span><span>saywrap</span><span>"</span><span>></span><span> <br> </span><span>21</span><span>.</span><span></span><span>php </span><span>echo</span><span>$sayList</span><span>;</span><span>?></span><span> <br> </span><span>22</span><span>.</span><span></span><span>div</span><span>></span></span></span></span></span></span></span></span></span></span></p>
至此,本例从前端交互到后台处理程序,全部完成,有兴趣的同学最好亲自动手,才能从中学到乐趣。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

1. Click [My] in the lower right corner of the Weibo app to enter your personal homepage. 2. Then, click the gear icon in the upper right corner. 3. At this time, we enter the settings page, select [Block Settings] here to enter. 4. In the blocking settings, we can see the friends we have blocked, tap it, and click [Unblock] in the pop-up options. 5. Or users can directly search Weibo users to enter the blocked user homepage, and click [...] on the upper right to remove the blacklist.

1. Open Weibo, click My, and then click the Settings option in the upper right corner. 2. After opening the settings, find and click on the push notification settings inside. 3. After entering the push notification settings, find Special Interest and click Real-time Notification.

1. First open Weibo, click Search, and click More Hot Searches. 2. Then select a hot search to enter. 3. Then enter the hot search comment live mode, find and click the standard version in the upper right corner. 4. Finally, you can return to the hot search standard comment area. If you want to continue viewing the live comment area, click the live version in the upper right corner. Note: Only when there are a lot of real-time comments will it automatically enter the live mode. Some hot searches will automatically enter the live mode. Generally, hot searches are in standard mode.

1. First open Weibo, click on My, and click on the draft box. 2. Then long press any draft and click to clear the draft box or delete the draft. 3. Finally click OK.

1. First open Weibo on your mobile phone and click [Me] in the lower right corner (as shown in the picture). 2. Then click [Gear] in the upper right corner to open settings (as shown in the picture). 3. Then find and open [General Settings] (as shown in the picture). 4. Then enter the [Video Follow] option (as shown in the picture). 5. Then open the [Video Upload Resolution] setting (as shown in the picture). 6. Finally, select [Original Image Quality] to avoid compression (as shown in the picture).

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and
