ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

AJAX + CodeIgniter

July 29, 2011 1:01am

Subscribe [3]
  • #1 / Jul 29, 2011 1:01am

    neovee

    2 posts

    Hi,

    right now I’m working on a website using CodeIgniter as its PHP Framework. I use AJAX to load content into the main page, when an user click one of the navigation link of my website. My aim is that the user won’t have to wait to reload the whole layout, but just partially, specifically only the content div. The link that are clicked will be processed by AJAX to a controller.

    I want this controller then process the link and sent back the data to the main page.

    For example, in my views folder I have a page ‘Background’ which is under the ‘About Us’ folder.

    Normally to load this page by CodeIgniter will need the user to type: “http://www.example.com/aboutus/background
    aboutus is my controller which has a background as its function and will load the page ‘Background’ in the views folder.

    Now I want that the link will be: “http://www.example.com/#/aboutus/background”, the hash will be needed for my AJAX request.

    My controller will receive only /aboutus/background from the AJAX and I want that my controller then will pull the ‘background’ Function from the ‘aboutus’ Controller, how do I achieve this? How do I build the code that will call function from another controller?

  • #2 / Jul 29, 2011 3:43am

    chewbacca

    3 posts

    Hello,
    I do this forever.

    Jquery

    //Navigation
    $("#SITE ul li a").click( function(e){
            e.preventDefault();
    
            var sitename = $(this).attr('href').substr(1).strtolower(); //strtolower is a Prototype
            $('.content').fadeOut('slow', function(){
                $('.content').html('');
                $('.content').load(BASE_URL  + 'your controller/siteload/' + sitename, function() {
                      
                        $('.content').fadeIn('slow', function(){     
                                //your Callback Function
                        });
                });
            });
    
            return false;
    });

    and Controller

    function siteload($site = NULL)
    {
        $this->uri_segment = addslashes( $site );   
        //before this i call my function isLive, then continue
        if( ! preg_match('/[^a-zA-Z]/', $this->uri_segment) )
        {       
            $this->_setOutput($this->uri_segment, $this->data);   
        }
        else
        {
            $this->index();
        }
    }
    
    function _setOutput($uri_segment = '', $data = '')
    {
        // load->view() is a return Function
        $this->load->view($uri_segment , $data);
    }
  • #3 / Jul 29, 2011 5:45am

    neovee

    2 posts

    Hello, thanks for your reply.

    I understand the jQuery part, but I still didn’t quide understand the controller part yet.

    Why $site=NULL? And why $uri_segment = ’‘, $data = ’‘?

    Then I still don’t know how to build the code to call/include a function from a different controller into the controller that I am working with.

    Like I’m in function A of controller B, how do I build the code to call/include the function X from controller Z?

  • #4 / Jul 29, 2011 6:10am

    chewbacca

    3 posts

    Ok, check out this http://ellislab.com/codeigniter/user-guide/general/urls.html.

    You can see “example.com/class/function/ID”. $site is the name from your view Template and $data is a array for your Data in your Template. I build this in constructor.

    “Like I’m in function A of controller B, how do I build the code to call/include the function X from controller Z?” this is a bad practice, better it you used libraries.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases