PHP简易表单验证与简易留言板实例扩展-V1.0
代嘎吼,我系Yangrl.
本次扩展优化了代码(好吧我在努力让它变好看╥﹏╥),增加分页功能。不足之处请您指出,一定马上改正并且给您个么么哒~上代码!
头部php:
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//初始化变量
$nickname=$title=$content="";
$nicknameError=$titleError=$contentError="";
function input_check($data) {
//去除字符(空格,tab,换行)
$data=trim($data);
//去除反斜杠\
$data=stripslashes($data);
//转为html实体
$data=htmlspecialchars($data);
return $data;
}
//如果访问页面使用的请求方法是POST,进行判断
if($_SERVER["REQUEST_METHOD"]=="POST") {
if(empty($_POST['username'])) {
$nicknameError="快点编个假名字好绝人!";
}else {
$nickname=input_check($_POST['username']);
}
if(empty($_POST['title'])) {
$titleError="留言还是要有个标题!";
}else {
$title=input_check($_POST['title']);
}
if(empty($_POST['content'])) {
$contentError="莫把内容都搞忘了哟!";
}else {
$content=input_check($_POST['content']);
}
//如果都不为空,就连接数据库插入留言信息
if(!empty($_POST['username']) && !empty($_POST['title']) && !empty($_POST['content'])) {
if(isset($_POST['releaseMsg'])) {
$user=trim(strip_tags($_POST['username']));
$title=trim(strip_tags($_POST['title']));
$content=trim(strip_tags($_POST['content']));
$host="localhost";
$username="root";
$password="root";
$dbname="ylc_mall";
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error) {
die("连接数据库失败!".$conn->connect_error);
}
$conn->set_charset("utf8");
$sql="INSERT INTO usermesg(mes_username,mes_title,mes_content,mes_createtime) VALUES ('{$user}','{$title}','{$content}','{$_SERVER['REQUEST_TIME']}')";
if($conn->query($sql)===TRUE) {
echo "<script>alert('留言成功!');location.href='ylc_messageBoard.php';</script>";
}else {
echo "<script>alert('留言失败!');location.href='ylc_messageBoard.php';</script>";
}
$conn->close();
}
}
}
?>
留言板部分(含分页操作):
<div class="mes_content">
<table class="mes_table" cellspacing="0" cellspacing="0">
<thead class="mes_thead">
<tr>
<th>
座位
</th>
<th>
昵称
</th>
<th>
标题
</th>
<th>
内容
</th>
<th>
发布时间
</th>
</tr>
</thead>
<tbody class="mes_tbody">
<?php
$host="localhost";
$username="root";
$password="root";
$dbname="ylc_mall";
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error) {
die("连接数据库失败!".$conn->connect_error);
}
//设定每页数据的条数
$pagesize=9;
//获取总记录数
$result=$conn->query("select * from usermesg");
$totaldatanum=$result->num_rows;
//计算总页数=总记录数/每页条数 ceil对结果取整
$totalpagesize=ceil($totaldatanum/$pagesize);
//当前页
$nowpage=isset($_GET['page']) ? ceil($_GET['page']) : 1;
//上一页
$prevpage=($nowpage-1<=0) ? 1 : $nowpage-1;
//下一页
$nextpage=($nowpage+1>=$totalpagesize) ? $totalpagesize : $nowpage+1;
//limit偏移值(每页条数)
$offset=($nowpage-1) *$pagesize;
//sql分页语句开始分页
$sql="select * from usermesg limit $offset,$pagesize";
$result=$conn->query($sql);
if ($result->num_rows>=0) {
while ($row=$result->fetch_object()) {
?>
<tr class="success">
<td>
<?php echo $row->mes_id; ?>
</td>
<td>
<?php echo $row->mes_username; ?>
</td>
<td class="mes_title_maxlen" title="<?php echo $row->mes_title; ?>">
<?php echo $row->mes_title; ?>
</td>
<td class="mes_content_maxlen" title="<?php echo $row->mes_content; ?>">
<?php echo $row->mes_content; ?>
</td>
<td>
<?php echo date("Y-m-d-H:i:s",$row->mes_createtime); ?>
</td>
</tr>
<?php
}
}else {
echo "无数据";
}
?>
</tbody>
</table>
<div class="mes_pagenum">
<ul>
<li><?php echo "<a href=\"?page=1\"> 首页 </a>"; ?></li>
<li><?php echo "<a href=\"?page=".$prevpage."\"> 上一页 </a>"; ?></li>
<li>
<?php
//for循环进行页数引导连接
for($i=1;$i<=$totalpagesize;$i++){
echo "<a href=\"?page=".$i."\"> 第".$i."页 </a>";
}
?>
</li>
<li><?php echo "<a href=\"?page=".$nextpage."\"> 下一页 </a>"; ?></li>
<li><?php echo "<a href=\"?page=".$totalpagesize."\"> 尾页 </a>"; $conn->close();?></li>
</ul>
</div>
<div class="mes_form">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<h2>发布</h2>
<div class="control-group">
<label class="control-label">昵称</label>
<div class="controls">
<input id="username" class="mes_inp" name="username" type="text" value="<?php echo $nickname; ?>" />
<span class="mes_error">*<?php echo $nicknameError; ?></span>
</div>
</div>
<div class="control-group">
<label class="control-label">标题</label>
<div class="controls">
<input id="title" name="title" class="mes_inp" type="title" value="<?php echo $title; ?>" />
<span class="mes_error">*<?php echo $titleError; ?></span>
</div>
</div>
<div class="control-group">
<label class="control-label">内容</label>
<div class="controls">
<textarea name="content" id="content" cols="50" rows="5" ><?php echo $content; ?></textarea>
<span class="mes_error">*<?php echo $contentError; ?></span>
</div>
</div>
<div class="mes_releasemsg">
<input type="submit" name="releaseMsg" class="mes_btn" value="发布" title="想好噢!">
</div>
</form>
</div>
</div>
最终效果:
分割线
接下来会继续扩展,想查看详情啊,回复啊,包括当前所在页这样的细节,let’s do it!
“真正有信心的人不怕暴露自己的缺点,试图掩盖粉饰才是有没有信心的表现。——龙应台”