Home >Backend Development >PHP Tutorial >How to detect if ci is submitting data via ajax or page post, ciajax_PHP tutorial

How to detect if ci is submitting data via ajax or page post, ciajax_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:26843browse

CI detects whether it is ajax or page post to submit data, ciajax

The example in this article describes the method of ci detecting whether the data is submitted by ajax or page post. Share it with everyone for your reference. The specific implementation method is as follows:

1. Question:

Because of the project needs, we want to know whether the source of the submitted data is the data submitted by ajax or the data submitted by the post of the page, so that we can process it at different levels.

2. Solution:

The solution in php is as follows:
If it is an ajax request, the value of the following expression is true

Copy code The code is as follows:
$_SERVER["HTTP_X_REQUESTED_WITH"]=="XMLHttpRequest"

It is a PHP environment variable.

How to handle it in ci:

Copy code 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');

I remember that when using THINKPHP, there are two built-in constants IS_AJAX and IS_POST. I want to use it in ci and I have searched for a long time but can’t find it. Then I can do it automatically
Add the above two lines of code to the project's config/constants.php configuration file. Now you can call it directly in all methods
For example:

Copy code The code is as follows:
if(IS_POST){
...
}
if(IS_AJAX){
...
}

I hope this article will be helpful to everyone’s CI framework programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/909350.htmlTechArticleHow to submit data using ci to detect whether it is ajax or page post, ciajax This article describes the example of ci detecting whether it is ajax or page post Method to submit data. Share it with everyone for your reference. Specific implementation...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn