Implement jquery ajax with codeigniter

Hello World, This is seventh tutorial of the series Learn Codeigniter From ScratchIn this turoail we will learn how to implement jquery ajax with codeigniter. We will apply jquery ajax on our existing todo application. I hope you are enjoying this tutorial. In the previous tutorial, we have learned that how we can implement pagination with codeigniter. In the previous tutorial we have used the codeigniter pagination library. I have divided this tutorial in following parts:

  • Overview concept in todo application
  • Implement jquery ajax
  • Test the application

Overview concept in todo application

In this tutorial we will implement jquery ajax on our existing todo application. In this application we will apply a task status button on task list. When someone click on this button then we will perform action that task is completed or pending. See the following screenshot.


In the above screen, you can see that we have added a button which shows that status is complete or not with colour. Lets apply the functionality with jquery ajax.

Implement jquery ajax with codeigniter

  • Open todo.php in application/views/todo folder and replace the previous code with following code.
<!DOCTYPE html>
	<title><?php echo $page_title; ?></title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url('public/bootstrap.min.css') ?>" />
	<!-- Include jquery -->
	<script type="text/javascript" src="<?php echo base_url('public/jquery-3.1.1.min.js') ?>"></script>
	<div class="container">
		<h1 class="page-header">To-Do List 
			<span class="badge"><?php echo $totalResult; ?></span>
			<a href="<?php echo site_url('todo/add'); ?>" class="btn btn-sm btn-danger pull-right">Add Task</a>
		<table class="table table-striped table-bordered">
				<th>Complete Action</th>
				foreach($content['d'] as $task){
						<td><?php echo $task['task_title']; ?></td>
						<td><?php echo $task['task_status']; ?></td>
						<td><button class="btn btn-<?php echo ($task['task_status']=='completed') ? 'success':'warning'; ?> btn-xs task-status-action" data-task_status="<?php echo $task['task_status']; ?>"><?php echo $task['task_status']; ?></button></td>
							<a href="#" class="btn btn-xs btn-primary">Edit</a>
							<a href="#" class="btn btn-xs btn-danger">Delete</a>
					<td colspan="3">
						<p class="alert alert-warning">No Data Found!!</p>
			echo $this->pagination->create_links();

In the above code, we have added button and jquery library.

  • Open Todo Controller and add the following method
// Ajax Method
	function ajax_change_status(){
		echo json_encode($this->Todo_model->ajax_change_status());
  • Open Todo model and add the following method in it.
	// Change Status via AJax
	function ajax_change_status(){
			$res['htmlData']='<button class="btn btn-'.$color.' btn-xs task-status-action" data-task_id="'.$task_id.'" data-task_status="'.$task_status.'">'.$task_status.'</button>';
			$res['htmlData']='<button class="btn btn-warning" btn-xs task-status-action" data-task_id="'.$task_id.'" data-task_status="'.$task_status.'">'.$task_status.'</button>';
		return $res;
  • Add the following code todo.php view file before closing tag of body.
		var ajaxUrl="<?php echo site_url(); ?>";
			var task_id=$(this).data('task_id');
			var task_status=$(this).data('task_status');
				var make_status='completed';
				var make_status='pending';
			var parentElm=$(this).parent();
			// Implement Jquery Ajax

Test the application

  • When you open the application in the browser, you will see the button and when you will click on button then you will see that status has been changed without loading the page.

Congratulations, You have successfully implement Jquery Ajax with codeigniter.  🙂

Leave a Reply

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