Codeigniter  JQuery Ajax

Posted on 2010-01-08
Last Modified: 2013-11-10
Hello Experts,

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.
Question by:bhefny
    LVL 1

    Expert Comment

    If I got you right your problem is, that you don't want to post your Ajax responses via views.

    I'm using Zend Framework for php and did it the following way:

    I use a single "Ajax Controller" thats catching alle the requests. It consist of one action for each request type (But that are usually between 5 and 10, beacuse you are able to use parameters within them).
    The controller creates the JSON or XML and after that I just print it directly from the controller with an echo() and the last line of code inside each controller is a die() to prevent the view of being called.
    Taht way I'vve got the the business logic where it should be (controller and model) and no unneccessary views. A couple of echo and die Statements that are conceald in a single controller (what with Zend Framework also means inside a single file) are a cheap price for that if you ask me ;-)

    Author Comment

    the first half sounds really logical and correct but I personally not in favor of writing HTML in controllers plus sometimes there are a lot of AJAX to be handled and there no point in writing 150 lines of HTML in a <?php echo '....';>

    So lets assume we implemented the first half and I have one controller for all.
    How can I have one view for all. What I have reached so far is have one view with a switch statment and let the controller pass the any incdentifier variable to it.
    Is there anything smarter than what I did?
    class C_ajax extends Controller {
        function fn_name(){
            $data['ajaxType'] = __FUNCTION__;
            // write any code here to be passed
            $this->load->view('news/v_news_ajax', $data);
            case 'fn_name':?>
                //write HTML here
            <?php break;?>
            <?php case '':?>
                // write HTML for another view here
            <?php break;?>
        <?php endswitch;?>

    Open in new window

    LVL 1

    Expert Comment

    You'll need another oppinion here, but I'll add another bit to mine:

    I'm not used to codeigniter. Does it support View Helpers? (Taht's the Zend Framewok way for reusing html/css/js code snippets).

    My views are actually doing barely more than conacting view helpers (maybe with one or two if/then control structures). That's why I havn't hundreds of lines of html code in my controller ;-)
    And some of the view helpers are doing the same thing (view helpers for forms are using vie helpers for input elements in the background).

    Maybe you could do something similar. Keep the switch case in the view and call a couple of view helpers from each case. That should keep the view very clean, and you should be able to reuse your html code frequently (if you create view helpers, taht cna be used in more than one case statement).

    Author Comment

    I don't think the question can should be deleted.
    I have provided a solution and someone else proposed a minor adjustment.

    This can help someone later on
    it's your call

    Accepted Solution

    Question PAQ'd, 500 points refunded, and stored in the solution database.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now