Codeigniter JQuery Ajax

I'm a heavy Codeignitor user and currently I have to build a page with extensive AJAX bits and pieces. I have been using JQuery all along the website and it's AJAX handling was perfect up until now. There is something that just doesn't feel right when i use a MVC with JQuery. For example: in Jquery I setup the callback URL easily that point to the controller, the controler then calls a view page and the AJAX is displayed but this view page is very small PHP code. So now I have about 40 ajax function to do in my page, would that mean I would have to make 40 views?? that doesn't seem right to me. Is there any better way to handle/manage the views created for the AJAX in MVC Frameworks

Thank you.


You could create one view, e.g. ajax.php and set the content accordingly in the methods of the controllers. All methods can use the same view.

The above is only true for similar views, but if the methods are similar as well, then you should think about refactoring your code to using uri parameters, see my answer to a question on how to get parameters from the url.

But all that depends on the nature of the controllers, their methods and the related views.


I do not advise you to use HTML in the controller, but lay out your controller like this:

Class My_Controller
    // some other methods
    public function edit_via_ajax($type, $value)
        /* do some stuff with $type and $value,
           sanitize user input etc. 
           set $status to TRUE or FALSE */
        $data['type'] = $type;
        $data['value'] = $value;
        if ($status === TRUE)
            $this->load->view('success.php', $data);
            $this->load->view('error.php', $data);

Create views like success.php

<?=$type;?> was successfully set to <?=$value;?>

And call it via $.get('/index.php/my_controller/edit_via_ajax/name/John_Doe'); in jquery to edit the name. In that way, lots of similar AJAX requests can share the same method and methods can share views.

Need Your Help

Styling all ListItems in an app, even those in a templated control

silverlight silverlight-4.0 coding-style custom-controls

I've create a style for ListItems that I want to use across all ListBoxes in my application. I can set these manually like so:

How to transition a gesture from a UIPinchGestureRecognizer handler to a UIPanGestureRecognizer handler?

ios touch uigesturerecognizer uipangesturerecognizer uipinchgesturerecognizer

I have a handler method for a UIPinchGestureRecognizer that is used to scale a view. What I'm trying to is to make it so that while two touches are on screen for the pinch gesture, and if a finger is

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.