一、项目效果展示
1、留言板效果展示

2、留言板搜索功能

二、步骤讲解
1、创建数据库
CREATE DATABASE onecms;
创建msg数据表
DROP TABLE IF EXISTS `msg`;
CREATE TABLE `msg` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
2、创建conn.inc.php
<?php
define("HOST",'localhost');
define("USER",'root');
define("PWD",'root');
define("DBNAME",'msg');
3、创建mysqli.php
<?php
include 'conn.inc.php';
$mysqli=new mysqli(HOST,USER,PWD,DBNAME);
if($mysqli->connect_errno){
die('数据库链接出错'.$mysqli->connect_error);
}
4、创建首页index.php
(1)引入kindeditor
kindeditor下载地址http://kindeditor.net/down.php

(2)添加js代码:(#content对应着html里内容标签的id属性)

<!DOCTYPE html>
<html>
<head>
<title>留言板</title>
<meta charset="UTF-8">
<script src="kindeditor/kindeditor-all.js"></script>
</head>
<body>
<div><h1>留言板</h1></div>
<div>
<form action="insertdb.php" method="post">
标题:<input type="text" id="title" name="title"><br>
内容:<br><span><textarea name="content" rows="13" cols="80" id="content"></textarea>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#content',{
afterBlur:function(){this.sync();}
})
});
</script>
</span>
<input type="submit" name="dosub" id="btn" value="上传留言">
</form>
</div>
</body>
</html>
3、初步效果

4、创建insertdb.php文件
<?php
include './mysqli.php';
header('Content-type:text/html;charset=utf-8');
$tit=$_POST["title"];
$con=$_POST["content"];
$sql="insert into msg(title,content) values('$tit','$con')";
if($mysqli->query($sql))
{
echo "留言成功,3秒后跳转原页面";
}else{
echo "留言失败,3秒后跳转原页面";
}
header("Refresh:3;url=index.php");
效果如下:

6、用ajax无刷新做添加功能,代码:
(1)先引入需要的js
<script src="jquery-1.11.0.js"></script>
(2)隐藏表单修改message.php代码:
<?php
//加载方法
var page=1;
var num=4;
$(function(){
load(page);
$("#btn1").click(function () {
var title=$("#title").val();
var content=$("#content").val();
$.post("insertdb.php",{title:title,content:content},function (data) {
if(data){
alert("留言成功!");
}else{
alert("留言失败请重新输入!");
}
load(page);
location.href="message.php";
},"text")
})
})
(3)修改insertdb.php代码如下:
相比之前删除了调转语句
<?php
include './mysqli.php';
$tit=$_POST["title"];
$con=$_POST["content"];
$sql="insert into message(title,content) values('$tit','$con')";
if($mysqli->query($sql))
{
//返回1表示添加成功
echo 1;
}
else{
//返回0表示添加失败
echo 0;
}
这就就实现了ajax无刷新Tina及留言
7、采用jquery来发送ajax请求
<!DOCTYPE html>
<html>
<head>
<title>留言板</title>
<meta charset="UTF-8">
<script src="jquery-1.11.0.js"></script>
<script src="kindeditor/kindeditor/kindeditor-all.js"></script>
<style>
#div1 div{height: 30px; line-height: 30px; padding-left: 10px; background: #f0f0f0; margin-bottom: 1px}
</style>
<script>
function load(){
var str="";
$.ajax({
type:'get',
url:'messageshowdb.php',
data:{},
dataType:'json',
success: function(data,status)
{
str="";
$.each(data, function(key,value) {
str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>";
$("#div1").html(str);
});
}
})
}
$(function(){
load(page);
$("#btn").click(function () {
var title=$("#title").val();
var content=$("#content").val();
$.post("insertdb.php",{title:title,content:content},function (data) {
if(data){
alert("留言成功!");
}else{
alert("留言失败请重新输入!");
}
load(page);
location.href="index.php";
},"text")
})
})
</script>
</head>
<body>
<div><h1>留言板</h1></div>
<div>搜索:<input id="con" name="sousuo"><input id="sousuo" type="button" value="确定"></div>
<br><div id="div1"></div>
<div>
标题:<input type="text" id="title" name="title"><br>
内容:<br><span><textarea name="content" rows="13" cols="80" id="content"></textarea>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#content',{
afterBlur:function(){this.sync();}
})
});
</script>
</span>
<input type="submit" name="dosub" id="btn1" value="上传留言">
</div>
</body>
</html>
8、新建messageshowdb.php
<?php
include 'mysqli.php';
$sql="select * from msg";
$result=$mysqli->query($sql);
if($result->num_rows>0)
{
while ($row=$result->fetch_assoc())
{
$arr[$row["id"]]["title"]=$row["title"];//$arr[1]["title"]=$row["title"]
$arr[$row["id"]]["content"]=$row["content"];//$arr[1]["content"]=$arr["content"]
}
}
echo json_encode($arr);
效果展示如下:

9、留言分页设计
(1)修改message.php代码:
<?php
var page=1;
var num=4;
function load(page){
function showpage() {
//获取总页数
$.ajax({
async:false,//设置成同步 函数内部变量 外部可以取到
type:'get',
url:'totaldb.php',
data:{page:page,num:num},
dataType:'text',
success: function(data,status)
{
totalpage=data;
}
})
var result="";
//显示分页
result="<input type='button' id='prev' value='上一页'></input>";
result+='当前页:第'+page+'页/总共:'+totalpage+"页";
result+="<input type='button' id='next'value='下一页'></input>";
$('#page').html(result);
//单击下一页 page递增 实现翻页
$('#next').click(function(){
page++;
if(page>=totalpage)
{
page=totalpage;
}
load(page);
})
//单击上一页 page递减 实现翻页
$('#prev').click(function(){
page--;
if(page<=1)
{
page=1;
}
load(page);
})
}
//获取内容
var str="";
$.ajax({
type:'get',
url:'messageshowdb.php',
data:{page:page,num:num},
dataType:'json',
success: function(data,status)
{
str="";
$.each(data, function(key,value) {
str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>";
$("#div1").html(str);
});
}
})
showpage();
}
(2)新建totaldb.php文件
<?php
include 'mysqli.php';
$num=$_GET["num"];//每页显示的个数
$sql="select * from msg";
$result=$mysqli->query($sql);
$totalnum=$result->num_rows;//总记录数
$totalpage=ceil($totalnum/$num);
echo $totalpage;
(3)修改messageshowdb.php
<?php
include 'mysqli.php';
$page= isset($_GET["page"])?$_GET["page"]:1;
$num=$_GET["num"];
$startnum=($page-1)*$num; //开始位置
$con=isset($_GET["con"])?$_GET["con"]:"";//搜索关键字
$content=isset($_GET["content"])?$_GET["content"]:"";
$sql="select * from message where 1=1";
$result=$mysqli->query($sql);
if($result->num_rows>0)
{
while ($row=$result->fetch_assoc())
{
$arr[$row["id"]]["title"]=$row["title"];//$arr[1]["title"]=$row["title"]
$arr[$row["id"]]["content"]=$row["content"];//$arr[1]["content"]=$arr["content"]
}
}
echo json_encode($arr);
(4)加入分页加载页面

(5)效果展示

10、留言搜索设计
(1)在留言板新加搜索按钮index.php文件中

<div>搜索:<input id="con" name="sousuo"><input id="sousuo" type="button" value="确定"></div>
(2)修改messageshowbd.php文件:

(3)效果展示

三、项目完整代码
1、项目所有文件

2、conn.inc.php
<?php
define("HOST",'localhost');
define("USER",'root');
define("PWD",'root');
define("DBNAME",'msg');
3、mysqli.php
<?php
include 'conn.inc.php';
$mysqli=new mysqli(HOST,USER,PWD,DBNAME);
if($mysqli->connect_errno){
die('数据库链接出错'.$mysqli->connect_error);
}
4、insertdb.php
<?php
include './mysqli.php';
$tit=$_POST["title"];
$con=$_POST["content"];
$sql="insert into msg(title,content) values('$tit','$con')";
if($mysqli->query($sql))
{
echo 1;
}
else{
echo 0;
}
header("Refresh:3;url=index.php");
5、totaldb.php
<?php
include './mysqli.php';
$num=$_GET["num"];//每页显示的个数
$sql="select * from msg";
$result=$mysqli->query($sql);
$totalnum=$result->num_rows;//总记录数
$totalpage=ceil($totalnum/$num);
echo $totalpage;
6、index.php
<!DOCTYPE html>
<html>
<head>
<title>留言板</title>
<meta charset="UTF-8">
<script src="jquery-1.11.0.js"></script>
<script src="kindeditor/kindeditor-all.js"></script>
<style>
#div1 div{height: 30px; line-height: 30px; padding-left: 10px; background: #f0f0f0; margin-bottom: 1px}
</style>
<script>
var page=1;
var num=8;
function load(page){
function showpage() {
//获取总页数
$.ajax({
async:false,//设置成同步 函数内部变量 外部可以取到
type:'get',
url:'totaldb.php',
data:{page:page,num:num},
dataType:'text',
success: function(data,status)
{
totalpage=data;
}
})
var result="";
//显示分页
result="<input type='button' id='prev' value='上一页'></input>";
result+='当前页:第'+page+'页/总共:'+totalpage+"页";
result+="<input type='button' id='next'value='下一页'></input>";
$('#page').html(result);
//单击下一页 page递增 实现翻页
$('#next').click(function(){
page++;
if(page>=totalpage)
{
page=totalpage;
}
load(page);
})
//单击上一页 page递减 实现翻页
$('#prev').click(function(){
page--;
if(page<=1)
{
page=1;
}
load(page);
})
}
//获取内容
var str="";
$.ajax({
type:'get',
url:'messageshowdb.php',
data:{page:page,num:num},
dataType:'json',
success: function(data,status)
{
//{
// "1":{"title":"t1","content":"c1"},
// "2":{"title":"t2","content":"c2"},
// "3":{"title":"t3","content":"c3"},
//}
str="";
$.each(data, function(key,value) {
str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>";
$("#div1").html(str);
});
}
})
showpage();
}
//加载方法
$(function(){
load(page);
$("#btn").click(function () {
var title=$("#title").val();
var content=$("#content").val();
$.post("insertdb.php",{title:title,content:content},function (data) {
if(data){
alert("留言成功!");
}else{
alert("留言失败请重新输入!");
}
load(page);
location.href="index.php";
},"text")
})
$("#sousuo").click(function () {
str="";
var con=$("#con").val();
$.get("messageshowdb.php?content=sousuo",{con:con,page:page,num:num},function (data) {
$.each(data, function(key,value) {
str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>";
$("#div1").html(str);
});
},"json")
showpage();
})
})
</script>
</head>
<body>
<div><h1>留言板</h1></div>
<div>搜索:<input id="con" name="sousuo"><input id="sousuo" type="button" value="确定"></div>
<br><div id="div1"></div>
<div id="page"></div>
<div>
标题:<input type="text" id="title" name="title"><br>
内容:<br><span><textarea name="content" rows="13" cols="80" id="content"></textarea>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#content',{
afterBlur:function(){this.sync();}
})
});
</script>
</span>
<input type="submit" name="dosub" id="btn" value="上传留言">
</div>
</body>
</html>
7、messageshowdb
<?php
include 'mysqli.php';
$page= isset($_GET["page"])?$_GET["page"]:1;
$num=isset($_GET["num"])?$_GET["num"]:4;
$startnum=($page-1)*$num; //开始位置
$con=isset($_GET["con"])?$_GET["con"]:"";//搜索关键字
$content=isset($_GET["content"])?$_GET["content"]:"";
$sql="select * from message where 1=1";
if($content=="sousuo"){
$sql.=" and title like '%$con%' or content like '%$con%'";
}
$sql.=" limit $startnum,$num";
$result=$mysqli->query($sql);
if($result->num_rows>0)
{
while ($row=$result->fetch_assoc())
{
$arr[$row["id"]]["title"]=$row["title"];//$arr[1]["title"]=$row["title"]
$arr[$row["id"]]["content"]=$row["content"];//$arr[1]["content"]=$arr["content"]
}
}
echo json_encode($arr);
网友评论