Overview of ThinkPHP page Jump success and error Methods

Source: Internet
Author: User

ThinkPHP itself provides the success and error Methods for page Jump with prompt information, which can display prompt information and jump effect after adding data. The success method is used for the prompt after the operation is successful, and the error method is used for the prompt after the operation fails. The two methods are the same. The success method is described below.
1. success Method
 
The success method syntax is as follows:
Success (message, ajax)
Parameter description message is optional. Page prompt information. Ajax is optional. Whether to submit data in AJAX mode. The default value is false.
If the request is submitted in AJAX mode, the success method calls the ajaxReturn method to return information.

Success Method example:

Public function insert () {// omit some other code if ($ lastInsId = $ Dao-> add ()) {// The target URL for page Jump $ 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 </A>] ');}

Success Template
 
The success method calls the success.html Template under TPL/Public/by default. In this template, the following template variables are received:
 
Template variable description:
$ WaitSecond jump wait time, in seconds. The default value is success 1 second and error 3 seconds.
$ JumpUrl: Jump to the target page address. The default value is $ _ SERVER ["HTTP_REFERER"] (the previous page in this tutorial ).
$ MsgTitle operation title. Note: the value assigned to this variable does not take effect. success takes the _ OPERATION_SUCCESS _ configuration information in the Language Pack, and the _ OPERATION_FAIL _ configuration information for error.
$ Message page prompt information.
$ Status indicates the operation status. The default value is 1, indicating that the operation is successful. The value 0 indicates that the operation fails. You can also define rules for the project.
 
Success still calls the display () Outputs output success.html template. For this reason, you can modify the success.html template, such as adding more information prompts or making CSS artist settings to meet actual needs. The preceding template variables are just some reference variables.
 
$ JumpUrl variable description:
If the $ jumpUrl variable does not exist, the default jump address is $ _ SERVER ["HTTP_REFERER"]. For some common jump page writing methods, refer:
 

// Other methods of this module, such as the index method $ this-> assign ("jumpUrl", "index"); // Add the parameter $ this-> assign ("jumpUrl ", "index/section /". $ section); // other modules $ this-> assign ("jumpUrl", "_ APP _/User/index ");

 
If you want to use the uessto generate a destination address, you can change the URL attribute to the U method format in success.html:

<meta http-equiv='Refresh'content='{$waitSecond};URL={:U($jumpUrl)}'>

 
In the operation, the value of the $ jumpUrl template variable must be changed to the same as that of the U method. For details, refer to the official manual ThinkPHPU method to dynamically generate a URL address.

2. error Method
 
The usage of the error method is exactly the same as that of the sucess method. However, you must note that the ThinkPHP connector still uses the success.html template to configure errorworkflow. If you need to define a template for the error method separately, you can modify the system default configuration ThinkPHP/Common/convention. php file as follows:

'Tmpl _ ACTION_ERROR '=> 'public: success', // default error jumps to the corresponding Template File

Or re-define in the project configuration.
In Web development, you can directly use the above success and error Methods provided by ThinkPHP page Jump when page Jump is required and prompt information is displayed. If you do not need to prompt but directly redirect the page, refer to the ThinkPHP redirect redirection section in the manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.