首页 > 代码库 > thinkphp留言板例子
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="http://www.mamicode.com/__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="http://www.mamicode.com/登录" 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 =http://www.mamicode.com/="ok") { window.location.href= "http://www.mamicode.com/__MODULE__/Main/xianshi";//__MODULE__:获取模块所在的路径 } } }); }); </script> </html>
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,"请登录..."); } } }
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,‘请登录...‘); } } }
主页面: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="http://www.mamicode.com/__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="http://www.mamicode.com/__CONTROLLER__/xiugai"><h1>发布信息</h1></a> </div> <div style="float:left; margin-left:30px"> <a href="http://www.mamicode.com/__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>
修改页面: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="http://www.mamicode.com/__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="http://www.mamicode.com/__CONTROLLER__/xianshi"><h1>查看信息</h1></a> </div> <div style="float:left; margin-left:30px"> <a href="http://www.mamicode.com/__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="http://www.mamicode.com/发送" id="btn" /><input type="reset" value="http://www.mamicode.com/复位" /></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 =http://www.mamicode.com/= ‘ok‘) { window.location.href="http://www.mamicode.com/__CONTROLLER__/xianshi"; } else if(datahttp://www.mamicode.com/=="no") { alert("您输入的内容不能为空"); } else { window.location.href="http://www.mamicode.com/__CONTROLLER__/xiugai"; } } }); }); </script> </body> </html>
主控制器(显示和修改):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); } }
__________
thinkphp留言板例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。