In this article, I’ll show you how to easily incorporate AJAX into your CodeIgniter application using jQuery.
As with most development, there are multiple ways to accomplish the sale goal. This is just one practical way to get the job done.
Step 1: Download the latest version of jQuery
Step 2: Create an /assets/js directory in the root of your application and add the jQuery file you just downloaded.
- /assets/js/jquery.1.4.2.min.js
Step 3: Include the jQuery file in your applications header template file.
- /apps/main/views/common/app_header.php
Tip: Define the path to your javascript directory in your parent controller.
- /apps/main/controllers/MY_Controller.php
// Update template data with app info. $this->tpl_data += array ( ... 'js_path' => base_url().'assets/js', ... );
Step 4: Create a controller that will display both the normal page and the AJAX widget.
- /apps/main/controllers/ajaxexample.php
class Jqueryexample extends MY_Controller {
// --------------------------------------------------------------------
/**
* The constructor
*/
function __construct()
{
parent::__construct();
}
// --------------------------------------------------------------------
/**
* The default method for this controller
*/
function index()
{
$this->tpl_data += array(
"current_dts" => date("Y-m-d: h:i:s"),
);
// Call the display template (/apps/main/views/jqueryexample.php)
$this->_tpl('jqueryexample');
}
// --------------------------------------------------------------------
/**
* This method will be called using AJAX
*/
function postoutput()
{
// TIP #1 -- Since this is a method that will be called via AJAX and the
// output will be inserted into a page that is already rendered, we need
// to use the $this->_widget which is define in the parent MY_Controller
// class.
//
// The _widget method simply does not include the header & footer but
// otherwise functions just like the _tpl method.
//
// TIP #2 -- If the profile is enabled in a parent controller, then I
// recommend that you disable it for AJAX methods to prevent profiler
// data from being displayed in your widgets.
$this->output->enable_profiler(FALSE);
$this->tpl_data += array(
"current_dts" => date("Y-m-d: h:i:s"),
);
// Call the display template (/apps/main/views/widgets/postoutput.php)
$this->_widget('widgets/postoutput');
}
// --------------------------------------------------------------------
}
Step 5: Add any of the AJAX calls you plan to use to one of your templates.
- See the jQuery documentation — http://api.jquery.com/category/ajax/
jQuery AJAX Example - Pulling in data from an controller using AJAX.
The current datetime is =$current_dts?>. Click Me
Step 6: Test it out…
CodeIgniter AJAX Example Before:
CodeIgniter AJAX Example After:
If you found this article useful, please share your comments below.


Pingback: Tweets that mention Simple CodeIgniter AJAX Example Using jQuery | Practical CodeIgniter Examples & Advice | gotphp.com -- Topsy.com
Pingback: Simple CodeIgniter AJAX Example Using jQuery | Practical … | Programming Blog Imagik.org