Rumah  >  Artikel  >  php教程  >  PHP+MySQL+jQuery实现发布微博程序PHP篇

PHP+MySQL+jQuery实现发布微博程序PHP篇

WBOY
WBOYasal
2016-06-06 19:36:201290semak imbas

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="images/</span><span>'</span><span>.</span><span>%24uid</span><span>.</span><span>'</span><span>.jpg"    style="max-width:90%"  style="max-width:90%" alt="demo"> <br>   7.      </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>

  至此,本例从前端交互到后台处理程序,全部完成,有兴趣的同学最好亲自动手,才能从中学到乐趣。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP缓存库phpFastCacheArtikel seterusnya:PHP搭建(windows64+apache2.4.7+mysql-5.6+php5.5+phpMyAdmin)

Artikel berkaitan

Lihat lagi