CI detection is an AJAX or page post submission method _php instance

Source: Internet
Author: User

This article illustrates how CI detection is a way to submit data in Ajax or page post. Share to everyone for your reference. The implementation methods are as follows:

First, the question:

Because the project requires that we want to know whether the data source is submitted by Ajax or the data submitted by the post of the page to be processed at different levels.

Second, the solution:

The workaround in PHP is as follows:
If it is an AJAX request, the value of the following expression is true

Copy Code code as follows:
$_server["Http_x_requested_with"]== "XMLHttpRequest"

is a PHP environment variable.

CI in the treatment method:

Copy Code code as follows:
Define (' Is_ajax ', isset ($_server[' Http_x_requested_with ']) && strtolower ($_server[' Http_x_requested_with ') ] = = ' XMLHttpRequest ');
Define ("Is_post", Strtolower ($_server[' request_method ') = = ' POST ');

Remember in the use of thinkphp has built-in two constants Is_ajax and Is_post, want to use in CI for half a day seems to have not found, then automatically hands-clothed
Add the top two lines of code to the config/constants.php configuration file of your project, and you can call it directly in all methods
For example:

Copy Code code as follows:
if (is_post) {
...
}
if (Is_ajax) {
...
}

I hope this article will help you with the CI Framework program design.

Related Article

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.