Thinkphp itself provides success method and error method to implement the page jump function with prompt information, which can realize the effect of displaying the information and jumping after adding data. The success method is used to prompt the successful operation, the error is used for the prompt after the operation failure, the two methods are identical, the following is explained by the success method.
1, Success method
The Success method syntax is as follows:
Success (message, Ajax)
The parameter description message is optional. Page tip information. Ajax Optional. Whether to commit in Ajax manner, default to False.
If it is an AJAX-style submission, the success method calls the Ajaxreturn method to return the information.
Success Method Example:
Public Function Insert () {
//omit part of other code
if ($lastInsId = $Dao->add ()) {
//page Jump Destination address
$this->assign ( "Jumpurl", "index");
$this->success (Insert Data ID: $lastInsId);
} else{
Header ("content-type:text/html; Charset=utf-8 ");
Exit ($Dao->geterror (). ' [<ahref= ' Javascript:history.back () ' > Return to </a>] ';
}
Success templates
The success method calls the public directory by default, which is the success.html template under tpl/public/. In the template, receive the following template variable:
Template Variable Description:
$waitSecond Jump wait time, in seconds, default success 1 seconds, error 3 seconds.
$JUMPURL jump to the destination page address, default to $_server["Http_referer"] (previous page of this operation).
$msgTitle the action title. Note: Assignment of this variable will not take effect, success will take _operation_success_ configuration information in the language pack, error _OPERATION_FAIL_ configuration information.
$message page tip information.
$status operation State, the default 1 indicates success, 0 indicates failure, and the rules can also be defined by the project itself.
Success is actually still calling the display () method output success.html template. Therefore, you can modify the success.html template according to the actual situation such as adding more information prompts or CSS art settings to meet the actual needs, the above template variables are only some reference variables.
$jumpUrl Variable Description:
$JUMPURL variable If it does not exist, the default jump address is $_server["Http_referer"], and some common jump pages are written for reference:
Other methods of this module, such as the index method
$this->assign ("Jumpurl", "index");
With parameters
$this->assign ("Jumpurl", "index/section/". $section);
Other modules
$this->assign ("Jumpurl", "__app__/user/index");
If you want to use the U method to generate a jump destination address, you can change the URL property to the U method form in success.html:
<meta http-equiv= ' Refresh ' content= ' {$waitSecond}; Url={:u ($JUMPURL)} ' >
In the operation, the assignment to the $jumpurl template variable should be changed to the same as the U method, and the official manual "Thinkphpu method dynamically generates the URL address" can be referenced in detail.
2. Error method
The error method usage is exactly the same as the Sucess method, but it should be noted that the thinkphp default configuration error method call is still a success.html template. If you need to define the template separately for the error method, you can modify the system default configuration thinkphp/common/convention.php file as follows:
' Tmpl_action_error ' => ' public:success ',//default error jump corresponding template file
or redefine it in the project configuration.
In web development, you can use the thinkphp page jump to provide the above success and error methods when you need a page to jump and display a hint message. If you do not need to be prompted but directly redirect the page, you can refer to the manual "thinkphp redirect Redirect" section.
More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."
I hope this article will help you with the PHP program design based on thinkphp framework.