Codeigniter Check Request is Ajax Example

Codeigniter Check Request is Ajax Tutorial. Sometimes, in a Codeigniter 3 application, we need to check if a request is made using AJAX. If we want to call the same method but perform something different if the request is AJAX, we can use the $this->input object.

Codeigniter 3 provides a method called is_ajax_request() in the $this->input object to check if a request is AJAX or not. Here’s an example of how it works:

You can check out the following example of a controller method to learn how to use is_ajax_request() and determine if a request is made using AJAX or not.

How to Check Ajax Request in Codeigniter

Here are example to check ajax request in codeigniter

public function ajaxRequestPost()
{
    if ($this->input->is_ajax_request()) {
   	
        $data = array(
            'title' => $this->input->post('title'),
            'description' => $this->input->post('description')
        );
        $this->db->insert('items', $data);
        echo 'Added successfully.';  
    }else{
        echo 'You can not access';
    }
}

Conclusion

Checking AJAX requests in CodeIgniter can benefit developers in many ways. By detecting AJAX requests, developers can modify server responses or perform different operations, enhancing the user experience. It can also improve the security of the application by preventing unauthorized access to sensitive data through AJAX calls. Overall, checking AJAX requests can provide more flexibility and control over the application’s behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *