This article mainly introduces how to use ci to detect ajax or page post to submit data. The PHP environment variables are used as the basic instance to demonstrate how to modify the CI framework configuration file, it is very practical. If you need it, you can refer to the examples in this article to describe how to use ci to detect ajax or page post to submit data. Share it with you for your reference. The specific implementation method is as follows:
I. Problems:
Because the project requires us to know whether the source of the submitted data is the data submitted by ajax or the data submitted by the page post for different levels of processing.
Ii. solution:
The solution in php is as follows:
For an ajax request, the value of the following expression is true.
The Code is as follows:
$ _ SERVER ["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
Is an environment variable of PHP.
Ci solution:
The Code is 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 that when THINKPHP is used, there are two built-in constants IS_AJAX and IS_POST. If you want to use them in ci for a long time, it seems that they have not been found, then you will be able to help yourself.
Add the above two lines of code to the config/constants. php configuration file of the project, and then you can directly call
For example:
The Code is as follows:
If (IS_POST ){
...
}
If (IS_AJAX ){
...
}
I hope this article will help you with CI framework programming.