thinkphp留言板例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp留言板例子相关的知识,希望对你有一定的参考价值。

登录:

login.html

技术分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script>
<title>登录页面</title>
<style type="text/css">
*{ margin:0px auto; padding:0px}
</style>
</head>

<body>
<div>用户名:<input type="text" id="user" /></div>
<div>密码:<input type="password" id="pass" /></div>
<div><input type="button" value="登录" id="btn" /></div>
</body>
<script type="text/javascript">
$("#btn").click(function(){
    var user = $("#user").val();    
    var pass = $("#pass").val();
    $.ajax({
    url:"__CONTROLLER__/yanzheng",    
    data:{User:user,Pass:pass},
    type:"POST",
    dataType:"TEXT",
    success: function(data)
    {
        if(data == "ok")
        {
            window.location.href= "__MODULE__/Main/xianshi";//__MODULE__:获取模块所在的路径
        }    
    }
    });    
});
</script>
</html>
View Code

LoginController.class.php

技术分享
<?php
namespace Liuyan\\Controller;
use Think\\Controller;
class LoginController extends Controller
{
    public function login()
    {
        $this->show();    
    }    
    public function yanzheng()
    {    /*echo "<script type=‘text/javascript‘>alert(‘aaa‘);</script>";*/
        $n = D("Yuangong");
        $uid = $_POST["User"];
        $pwd = $_POST["Pass"];
        $sql = "select pass from yuangong where user=‘{$uid}‘";
        $r = $n->query($sql);
        //var_dump($r[0][pass]);
        if(!empty($r[0][pass]) && $r[0][pass]==$pwd)    //$r是一个二维数组,$r[0][pass]取出密码(string类型)
        {
            session("user",$uid);                //登录之后,存session
            $this->ajaxReturn("ok","eval");        //登录成功,ajax返回ok到login.html页面,进行下一步操作
        }
        else if(empty($r[0][pass]) || $r[0][pass] != $pwd)//如果密码为空或者输入的密码和数据库中此用户名的密码不匹配,返回登录页面
        {
            $this->redirect("Liuyan/Login/login","array()",3,"请登录...");    
        }
    }
}
View Code

ParentController.class.php:中间过渡控制器;parent::__construct(); //在构造函数里面加这一句,防止报错

技术分享
<?php
namespace Liuyan\\Controller;
use Think\\Controller;
class ParentController extends Controller
{
    public function __construct()
    {
        parent::__construct();        //在构造函数里面加这一句,防止报错
        if(session(‘?user‘))
        {
        }
        else
        {
            $this->redirect(‘Liuyan/Login/login‘,array(),3,‘请登录...‘);    
        }    
    }    
}
View Code

主页面:xianshi.html

技术分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script><!--引入jQuery文件-->
<title>主页面    </title>
<style type="text/css">
*{ margin:0px auto; padding:0px}
</style>
</head>

<body>
<div>
    <div style="float:left">
        <a href="__CONTROLLER__/xiugai"><h1>发布信息</h1></a>
    </div>
    <div style="float:left; margin-left:30px">
        <a href="__CONTROLLER__/qing" id="aa"><h1>退出系统</h1></a>
    </div>
</div><br /><br /><br />

<div>
<span><h3>留言信息</h3></span>
<table width="60%" border="1" cellpadding="0" cellspacing="0" id="tb">   
</table>
</div>
<div style="height:20px;">{$btn}</div>
<script type="text/javascript">
$(document).ready(function(e) {
$.ajax({
url:"__CONTROLLER__/yemian",    
data:{},
type:"POST",
dataType:"JSON",
success: function(data)
{    
    var str = "<tr><td>发送人</td><td>发送时间</td><td>接收人</td><td>信息内容</td></tr>";
    for(a in data)    //JSON接受循环显示数据方法
    {
        str+="<tr><td>"+data[a].sender+"</td><td>"+data[a].times+"</td><td>"+data[a].recever+"</td><td>"+data[a].comment+"</td></tr>";    
    }    
    $("#tb").append(str);
}
});
});
</script>
</body>
</html>
View Code

修改页面:xiugai.html

技术分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script><!--引入jQuery文件-->
<title>发布信息</title>
<style type="text/css">
*{ margin:0px auto; padding:0px}
</style>
</head>

<body>
<div>
<div style="float:left">
<a href="__CONTROLLER__/xianshi"><h1>查看信息</h1></a>
</div>
<div style="float:left; margin-left:30px">
<a href="__CONTROLLER__/qing" id="aa"><h1>退出系统</h1></a>
</div>
</div><br /><br /><br />

<div>
    <div>
        信息发送:
    </div>
    <div>
        <div>
        <span>接收人:</span>
       <select id="inp">
       <foreach name="a" item="v" >   
       <option>{$v.firend}</option>
        </foreach>
       </select>
        </div>
        
        <div>
        <span>信息内容:</span><textarea name="comment" id="tx"></textarea>
        </div>
    
    </div>
    <div><input type="button" value="发送" id="btn" /><input type="reset" value="复位" /></div>
</div>
<script type="text/javascript">
$("#btn").click(function(){
var inp = $("#inp").val();
var tx = $("#tx").val();
$.ajax({
url:"__CONTROLLER__/addxian",
data:{inp:inp,tx:tx},
type:"POST",
dataType:"TEXT",
success: function(data)
{
    if(data == ‘ok‘)
    {
        window.location.href="__CONTROLLER__/xianshi";    
    }
    else if(data=="no")
    {
        alert("您输入的内容不能为空");    
    }
    else
    {
        window.location.href="__CONTROLLER__/xiugai";    
    }    
}    
});
});
</script>
</body>
</html>
View Code

主控制器(显示和修改):MainController.class.php

技术分享
<?php
namespace Liuyan\\Controller;
use Liuyan\\Controller\\ParentController;
class MainController extends ParentController
{
    public function xianshi()
    {
        $this->show();    
    }
    public function yemian()
    {
        $n = D("liuyan");
        $zs = $n->count();    //求数据总数
        $p = new \\Liuyan\\fzl\\Page($zs,3);
        $attr = $n->limit($p->limit)->select();    //echo $p->limit;输出为0,2,直接放到$n->limit()中
        $btndiv = $p->fpage();    //翻页按钮
        $this->assign("btn",$btndiv);//翻页按钮注册到前台显示
        $this->ajaxReturn($attr);
    }    
    public function xiugai()    //发布信息页面收件人下拉列表显示
    {
        $n = D("Firend");
        $a = $n->select();
        $this->assign("a",$a);
        $this->show();    
    }
    public function addxian()    //$data["ids"]:添加到数据库的列数据
    {
        $data["Ids"]="";                    
        $data["Sender"] = session("user");
        $data["Recever"] = $_POST["inp"];
        $data["Times"] = date("Y-m-d H:i:s");
        $data["Comment"] = $_POST["tx"];
        $data["States"]="";
        $n = D("Liuyan");
        if(empty($_POST["inp"]) || empty($_POST["tx"]))
        {
            echo "no";    
        }
        else
        {
            $r = $n->add($data);    //添加数据库
            if($r)
            {
                echo "ok";    
            }
        }
    }
    public function qing()        //点击退出系统,清session
    {
        session(null);
        $this->redirect("Liuyan/Login/login",array(),0);    
    }
}
View Code

__________

以上是关于thinkphp留言板例子的主要内容,如果未能解决你的问题,请参考以下文章

gitlab 权限说明

golang goroutine例子[golang并发代码片段]

超级有用的9个PHP代码片段

分享几个实用的代码片段(附代码例子)

分享几个实用的代码片段(附代码例子)

ThinkPHP中使用PHPMailer发送邮件