Heim  >  Artikel  >  Backend-Entwicklung  >  ThinkPHP跳转页success及error模板实例教程_PHP

ThinkPHP跳转页success及error模板实例教程_PHP

WBOY
WBOYOriginal
2016-06-01 11:50:03735Durchsuche

本文以实例讲解了ThinkPHP跳转页面的success与error方法所对应的视图与控制器的实现方法,通过本实例教程可以帮助读者更好的掌握success方法与error方法的使用。

首先是控制器中,可以使用下代码:

<&#63;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();
  }
}
&#63;>

success.html 页面 位置:Tpl\default\Public\success.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页面 位置:Tpl\default\Public\success.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方法返回信息

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn