本文以实例讲解了thinkphp跳转页面的success与error方法所对应的视图与控制器的实现方法,通过本实例教程可以帮助读者更好的掌握success方法与error方法的使用。
首先是控制器中,可以使用下代码:
<?php
// 本文档自动生成,仅供测试运行
class indexaction extends action
{
/**
+----------------------------------------------------------
* 默认操作
+----------------------------------------------------------
*/
public function index()
{
$this->assign("title","净水器");
$user = m("user"); // 实例化user对象
$result = $user->add($data);
if ($result){
// 设置成功后的跳转页面地址 默认的返回页面是$_server["http_referer"]
$this->assign("jumpurl","index");
$this->success("新增成功!");
}else{
// 错误页面的默认跳转页面是返回上一页 通常可以不用设置
$this->error("新增错误!");
}
$this->display();
}
}
?>
success.html 页面 位置:tpldefaultpublicsuccess.html
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="200; url='{$url}'" />
<title>成功提示信息</title>
</head>
<body>
<table width="500" border="1" align="center" cellpadding="3" cellspacing="0">
<tr>
<th align="center" bgcolor="#cccccc">成功信息提示</th>
</tr>
<tr>
<td>{$message}<br />
成功处理的标题信息:{$msgtitle}<br />
<present name="message">
自定义成功信息:{$message}<br />
</present>
<present name="error">
自定义错误信息:{$error}<br />
</present>
跳转页面路径:{$jumpurl}<br />
停留时间:{$waitsecond}<br />
2秒后返回指定页面!<br />
如果浏览器无法跳转,<a href="__root__/{$jumpurl}" rel="external nofollow" rel="external nofollow" >请点击此处</a>。</td>
</tr>
</table>
</body>
</html>
error页面 位置:tpldefaultpublicsuccess.html
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="2; url='{$url}'" />
<title>成功提示信息</title>
</head>
<body>
<table width="400" border="1" align="center" cellpadding="3" cellspacing="0">
<tr>
<th align="center" bgcolor="#cccccc">错误信息提示</th>
</tr>
<tr>
<td>{$message}<br />
成功处理的标题信息:{$msgtitle}<br />
<present name="message">
自定义成功信息:{$message}<br />
</present>
<present name="error">
自定义错误信息:{$error}<br />
</present>
跳转页面路径:{$jumpurl}<br />
停留时间:{$waitsecond}<br />
2秒后返回指定页面!<br />
如果浏览器无法跳转,<a href="__root__/{$jumpurl}" rel="external nofollow" rel="external nofollow" >请点击此处</a>。</td>
</tr>
</table>
</body>
</html>
success和error方法都有对应的模板,并且是可以设置的,默认的设置public:success和public:error,模板文件可以使用模板标签,并且可以使用下面的模板变量:
$msgtitle :操作标题
$message :页面提示信息
$status :操作状态 1表示成功 0 表示失败 具体还可以由项目本身定义规则
$waitsecond :跳转等待时间 单位为妙
$jumpurl :跳转页面地址
如果是ajax方式提交的话,success和error方法会调用ajaxreturn方法返回信息
更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《smarty模板入门基础教程》及《php模板技术总结》。
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!