Codeigniter JQuery Ajax Request Example

By Hardik Savani November 5, 2023 Category : Codeigniter Ajax

In this post, i would like to share with you how to send ajax post request with data to codeigniter controller example. Here in full example we will also check for ajax request using is_ajax_request and send post request using jquery.

jQuery Ajax methods really made easy to post or get a data and return that data without refreshing the page. it's really amazing. We will apply this jQuery Ajax post in CodeIgniter 3 project. So it will help you to make better codeigniter project.

So, in this article we will create new table "items" and then we simple list out that column in view file. then will write ajax request code and it will fire ajax post request from view file and save data in database.

So just follow bellow step and get simple example:

Step 1: Make Table

In first table we must have one table with some dummy records. For this example i created "items" table, so run bellow query:



`title` varchar(255) NOT NULL,

`description` varchar(255) NOT NULL,

`created_at` datetime NOT NULL,

`updated_at` datetime NOT NULL,



Step 2: Create Route

In this step we need to add one route for ajax search data and another for view. so open routes.php file and add code like as bellow:


$route['default_controller'] = 'welcome';

$route['404_override'] = '';

$route['translate_uri_dashes'] = FALSE;

$route['ajax-request'] = 'ItemController/ajaxRequest';

$route['ajax-requestPost']['post'] = 'ItemController/ajaxRequestPost';

Step 3: Create Controller

In Last step we have to create ItemController Controller, in this file we will write search database logic.

So, create new ItemController.php file in controllers folder and put bellow code:



defined('BASEPATH') OR exit('No direct script access allowed');

class ItemController extends CI_Controller {


* Get All Data from this method.


* @return Response


public function __construct() {





* Get All Data from this method.


* @return Response


public function ajaxRequest()


$data['data'] = $this->db->get("items")->result();

$this->load->view('itemlist', $data);



* Get All Data from this method.


* @return Response


public function ajaxRequestPost()


$data = array(

'title' => $this->input->post('title'),

'description' => $this->input->post('description')


$this->db->insert('items', $data);

echo 'Added successfully.';



Step 4: Make View File

In second step, we have to create view file, If you installed fresh codeigniter then we need to create itemlist.php on views folder and put bellow code on that file.


<!DOCTYPE html>



<title>codeigniter ajax request -</title>

<link rel="stylesheet" href="" />

<script src=""></script>



<div class="container">

<div class="row">

<div class="col-lg-12 margin-tb">

<div class="pull-left">

<h2>codeigniter ajax request -</h2>




<div class="row">

<div class="col-lg-8">


<input type="text" name="title" class="form-control" placeholder="Title">


<div class="col-lg-8">


<textarea name="description" class="form-control" placeholder="Description"></textarea>


<div class="col-lg-8">


<button class="btn btn-success">Submit</button>



<table class="table table-bordered" style="margin-top:20px">








<?php foreach ($data as $item) { ?>


<td><?php echo $item->title; ?></td>

<td><?php echo $item->description; ?></td>


<?php } ?>




<script type="text/javascript">


var title = $("input[name='title']").val();

var description = $("textarea[name='description']").val();


url: '/ajax-requestPost',

type: 'POST',

data: {title: title, description: description},

error: function() {

alert('Something is wrong');


success: function(data) {


alert("Record added successfully");







Now we are ready to check, so you can check your own.

I hope it can help you....