PHP+Mysql+jQuery实现发布微博程序 php篇,mysqljquery
先还是要说明本例的业务流程:
1、前端用户输入内容,并对输入的内容字数进行实时统计。
2、用户提交数据,jQuery实现通过Ajax向后台发送数据。
3、后台PHP接收提交表单的数据,并对数据进行必要的安全过滤。
4、后台PHP连接Mysql数据库,并将提交过来的表单数据写入到相应的数据表中。
5、后台向返回成功结果数据内容,并通过Ajax将返回的数据内容插入到前端页面中。
上述1、2步在前篇文章:jQuery篇已讲解了,本文将完成剩余的散步。
效果图:
数据表
首先我们要准备一个数据表,表结构如下:
CREATE TABLE `say` ( `id` int(11) NOT NULL auto_increment, `userid` int(11) NOT NULL default '0', `content` varchar(200) NOT NULL, `addtime` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
注意,本例中将时间字段:addtime的类型设置为int,是为了后续的时间处理方便,在很多应用中(如Discuz论坛)都是将时间类型转成数字型。
时间轴处理函数和格式化输出列表函数:
时间轴处理函数,就是把时间转换成我们看到的诸如“5分钟前”,“昨天 10:21”等形式,代码如下:
/*时间转换函数*/ function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date("H:i",$time); $time = time() - $time; if ($time < 60) { $str = '刚刚'; } elseif ($time < 60 * 60) { $min = floor($time/60); $str = $min.'分钟前'; } elseif ($time < 60 * 60 * 24) { $h = floor($time/(60*60)); $str = $h.'小时前 '.$htime; } elseif ($time < 60 * 60 * 24 * 3) { $d = floor($time/(60*60*24)); if($d==1) $str = '昨天 '.$rtime; else $str = '前天 '.$rtime; } else { $str = $rtime; } return $str; }
格式化输出函数是将得到的用户信息和发布内容及时间按照一定的格式输出到前端页面的函数,代码如下:
function formatSay($say,$dt,$uid){ $say=htmlspecialchars(stripslashes($say)); return' <div class="saylist"><a href="#"><img src="/static/imghwm/default1.png" data-src="images/'.$uid.'.jpg" class="lazy".$uid.'.jpg" style="max-width:90%" style="max-width:90%" alt="demo" /></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="$1" rel="nofollow" target="blank">$1</a>',$say).' </p><div class="date">'.tranTime($dt).'</div> </div> <div class="clear"></div> </div>'; }
将以上两个函数都放入function.php中,准备随时被调用。
submit.php处理表单数据
在之前文章中,我们知道jQuery将前端获得的数据以POST方式,通过Ajax提交给了后台的submit.php。那么submit就是要完成后续的所有一摊子任务。请看代码:
require_once('connect.php'); //数据库连接文件 require_once('function.php'); //函数调用文件 $txt=stripslashes($_POST['saytxt']); //获取提交的数据 $txt=mysql_real_escape_string(strip_tags($txt),$link); //过滤HTML标签,并转义特殊字符 if(mb_strlen($txt)<1 || mb_strlen($txt)>140) die("0"); //判断输入字符数是否符合要求 $time=time(); //获取当前时间 $userid=rand(0,4); //插入数据到数据表中 $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); //调用函数输出结果
注意,本例中为了演示,将用户ID(userid)进行随机处理,实际的应用是获取当前用户的ID。另外数据库连接文件,大家可以自己写一个,在我提供的下载的DEMO里也有这个文件。
最后要回到前端页面index.php来。index.php主要除了提供输入的入口,还要承接后台处理返回的结果,并且要将数据库里已有的数据显示出来。代码如下:
<?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"> <h3 id="span-class-counter-span-说说你正在做什么"><span class="counter">140</span>说说你正在做什么...</h3> <textarea name="saytxt" id="saytxt" class="input" tabindex="1" rows="2" cols="40"></textarea> <p> <input type="submit" class="sub_btn" value="提 交" disabled="disabled" /> <span id="msg"></span> </p> </form> <div class="clear"></div> <div id="saywrap"> <?php echo $sayList;?> </div>
以上就是本文的全部内容,希望对大家的学习有所帮助。

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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.