Using jQuery and ajax with Codeigniter

In previous posts I have already written about how to get database information using jQuery, ajax and PHP. Today, I would like to integrate that same approach while using the codeigniter framework. Take a look at it here, if you haven’t read it yet.

To integrate jQuery with codeigniter, might sound a bit scary at first, but mind you, it’s not something out of this world. We will implement the same example from the previous jQuery post. We will use the same html page used in previous post of ajax jquery, and will only change how our PHP script will look, making it specific for codeigniter. Thus, making this post a bit vague for those that do not know how this works yet. Again, I recommend all that have not read my previous post, to do so.

Only one change needs to be done to this page, and it lies within the ajax call. Since we are using codeigniter, then we all know that the standard way of calling a function and a method is through the url and looks something like, the base_url / controller / function / additional parameters. In this particular example, we will create a new controller named profiles. Within the controller, we are going to have a function named: get_all_users(), so in this html page, we are going to change the ajax url parameter to this:

PHP

1

url:'<?phpechobase_url().'profiles/get_all_users';?>'

This should return something like this:

1

http://www.yourdomain.com/index.php/products/get_all_users

Or if you have removed the index.php then:

1

http://www.yourdomain.com/products/get_all_users

Now, instead of having that ‘getdata.php’ file, we will create the products controller file for this task we are going to run. It will basically do the same process, except in a ‘codeigniter’ kind of way. Let’s create it:

Note: for the sake of this tutorial, I will place my logic within the controller, but the best practices are to create your logic within a model.

File name: products.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<?php

classProductsextendsCI_Controller{

function__construct(){

parent::__construct();

}

publicfunctionget_all_users(){

$query=$this->db->get('products');

if($query->num_rows>0){

$header=false;

$output_string='';

$output_string.="<table border='1'>\n";

foreach($query->result()as$row){

$output_string.='<tr>\n';

$output_string.="<th>{$row['value']}</th>\n";

$output_string.='</tr>\n';

}

$output_string.='</table>\n';

}

else{

$output_string='There are no results';

}

echojson_encode($output_string);

}

}

?>

This logic is a bit simpler than the original post, but it’s just to get the idea on how to create an ajax, jquery connection using codeigniter. The html page should look something like this:

Well, it’s actually for the sake of simplicity. I have noticed that many people ask about this, so from now on, I’m going to make a bit more comprehensive tutorials concerning the structures I talk about.

when I am loading database class this error occured – “Fatal error: Cannot use object of type stdClass as array in D:\xampp\htdocs\cirnd\application\controllers\Products.php on line 18
”
Please tell me how can run this programme.

I think there is an error. Isn’t the controller supposed to be called “Profiles” instead of “Products”? There are many places where you write “products” whereas the “get_all_users” method should be relevant to the “profiles”.

Hi,
I wanted to know one thing for clear from the code above. With or without a framework, will I be able to call a function / method within class? I have searched many forums (SO for example) and I have come across only one answer – it cannot be done. I even tried and all I got was a 404 error. Amazingly, there are lot of answers for ASP .NET while calling a method using $.ajax. Why not for PHP ? I have see many plugins (if you can call it so) like phery.js but somehow I am not convinced. Also the piece of code in your example is within the view. I prefer to have my js code as external and include it at the bottom on the view. Now will the $.ajax call in your example work if it is in an external js file.?