ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
分类:
IT文章
•
2022-04-03 17:14:32
一、ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串)
1.MainController.class.php
<?php
namespace AjaxtestController;
use ThinkController;
class MainController extends Controller
{
public function zhuye()
{
$this->show();
}
public function addchuli()
{
$n = D("Nation"); //造对象
$n->create(); //POST传送,自动收集表单
$r = $n->add();
if($r)
{
$this->ajaxReturn("ok","eval"); //ajax的返回方法,eval返回字符串
}
else
{
$this->ajaxReturn("no","eval");
}
}
}
View Code
2.zhuye.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><!--__PUBLIC__引入jQuery文件-->
<title>Ajax主页面</title>
</head>
<body>
<div>代号:<input type="text" id="code" /></div>
<div>名称:<input type="text" id="name" /></div>
<input type="button" id="btn" value="添加" />
</body>
<script type="text/javascript">
$("#btn").click(function(){
var code = $("#code").val();
var name = $("#name").val();
$.ajax({
url:"__CONTROLLER__/addchuli", //控制器路径的addchuli()方法
data:{Code:code,Name:name}, //大小写要和数据库对应
type:"POST",
dataType:"TEXT",
success: function(data)
{
if(data == "ok")
{
window.location.href="__CONTROLLER__/zhuye";//添加成功后返回到显示页面,走控制器的zhuye()方法
}
}
});
})
</script>
</html>
View Code
二、分页
*把要使用的类放到根目录下,如Fenye(模块)/fzl(模块下自定义文件夹)/Page.class.php,要在类里面写上命名空间
*如何在控制器里面引入第三方类:
1.FenyeController.class.php
<?php
namespace FenyeController;
use ThinkController;
class FenyeController extends Controller
{
public function zhuye()
{
$n = D("chinastates");
$zs = $n->count(); //求表的总条数
$p = new FenyeFzlPage($zs,15);//引入Page.class.php文件,造一个实例化对象,Page(总条数,每页几条)
//$p->limit;//echo $p->limit;输出的是LIMIT 0,15;如何把LIMIT去掉?需要在Page.class.php中的56行做下修改,就可以了
$btndiv = $p->fpage();//把上下翻页的按钮显示出来
$attr = $n->limit($p->limit)->select(); //$p->limit就是一个字符串(0,15),直接放在limit()里面就可以
$this->assign("china",$attr);//注册chinastates表的信息,到html调用
$this->assign("btn",$btndiv);//注册上下翻页按钮,到html调用
$this->show();
}
}
View Code
2.zhuye.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" />
<title>无标题文档</title>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>地区代号</td>
<td>地区名称</td>
<td>父级代号</td>
</tr>
<foreach name="china" item="v" >
<tr>
<td>{$v.areacode}</td>
<td>{$v.areaname}</td>
<td>{$v.parentareacode}</td>
</tr>
</foreach>
</table>
<div>{$btn}</div>
</body>
</html>
View Code
三、自动加载函数库(functions):可以把第三方的一些比较好用的函数,放到functions里面,需要用的时候可自动加载
1.建一个functions.php文件,路径为'模块/Common/functions.php'
2.在Jiazai这个模块下的Conf文件夹下修改配置文件config.php
3.例子(求字符串的长度)
functions.php
<?php
function CD($str)
{
return strlen($str);
}
JiazaiController.class.php
<?php
namespace JiazaiController;
use ThinkController;
class JiazaiController extends Controller
{
public function jiazai()
{
$str = "/* 数据库设置 */";
echo CD($str); //调用函数库的CD方法
}
}
四、session如何防止跳过登录访问
*在模块下面建一个ParentController.class.php控制器,这个控制器的作用是在它的平级模板(比如:MainController.class.php控制器)和think里的Controller之间做一个过渡控制器,顺序是:MainController.class.php继承自ParentController.class.php,而ParentController.class.php继承自Controller,在ParentController.class.php里面写一个构造函数,这个构造函数的作用是用户访问各控制器(Main)时,先走ParentController.class.php,判断session是否存在,若不存在,跳转登录,若存在,可以访问
*构造函数:
PHP构造函数的声明与其它操作的声明一样,只是其名称必须是__construct( )。这是PHP5中的变化,以前的版本中,构造函数的名称必须与类名相同,这种在PHP5中仍然可以用,但现在以经很少有人用了,这样做的好处是可以使构造函数独立于类名,当类名发生改变时不需要改相应的构造函数名称了。为了向下兼容,如果一个类中没有名为__construct( )的方法,PHP将搜索一个php4中的写法,与类名相同名的构造方法。格式:function __construct ( [参数] ) { … … }在一个类中只能声明一个构造方法,而是只有在每次创建对象的时候都会去调用一次构造方法,不能主动的调用这个方法,所以通常用它执行一些有用的初始化任务。比如对成属性在创建对象的时候赋初值。
LoginController.class.php
<?php
namespace LoginController;
use ThinkController;
class LoginController extends Controller
{
public function login()
{
session("username","zhangsan"); //测试,给定一个session
}
}
View Code
ParentController.class.php
<?php
namespace LoginController;
use ThinkController;
class ParentController extends Controller
{
//构造函数
public function __construct()
{
parent::__construct();
if(session('?username'))
{
}
else
{
$this->redirect('Login/Login/login',array(),3,'请登录...');
}
}
}
View Code
<?php
namespace LoginController;
use ThinkController;
class ParentController extends Controller
{
//构造函数
public function __construct()
{
if(session('?username'))
{
}
else
{
$this->redirect('Login/Login/login',array(),3,'请登录...');
}
}
}
MainController.class.php
<?php
namespace LoginController;
use LoginControllerParentController; //注意书写格式
class MainController extends ParentController //随便一个控制器
{
public function test()
{
}
}
View Code