user3384074
user3384074

Reputation: 35

Codeigniter - refresh data sent to view with jQuery

I have no idea how to refresh the data sent to the view using jquery without refreshing the entire web site. I wrote something like this:

Controller:

public function blog($id) {

$data['comments'] = $this->Comments->findall('blog', $id);
$this->load->view('sites/blog', $data);

}

Model:

... reading from database ...

View:

<div id="comments">

reading data from $comments sent by controller blog

</div>

How can I refresh DIV comments using jQuery in the view that the function in the controller BLOG read a new refreshed number of comments? If I put in DIV comments code:

<? $this->load->view('sites/comments'); ?>

And refresh this DIV using jQuery this is unsuccessful because the data sent by the controller remain unchanged :/

Upvotes: 2

Views: 9674

Answers (4)

Vickel
Vickel

Reputation: 7997

I like to use the following approach:

1st: create a view main.php with a <div id="blog_container"></div>. main.php is loaded from your default controller.

2nd: create a view blogs.php, where you'll output your blog data like:

view blogs.php

<div class="comments">
    <?=$comments?>
</div>

3rd: add a jquery ajax function to main.php, calling a controller function e.g. get_blogs(). This function queries your blog data and loads blogs.php, but with a trick: you'll set it to true, so the output is a html-string

ajax function:

   function refresh_blog(){
      $.ajax({
        url:        url to your controller,
        type:       'POST',
        cache:      false,
        success: function(html){                    
            $('#blog_container').html(html)
        }           
      });
   }

controller get_blogs:

get_blogs(){
    // below would better be in a model
    $html='';
    $query = $this->db->get('my_blogs');        
    $blogs = $query->result();

    foreach ($blogs as $row){
       // data from your table my_blogs column comments
          $data['comments']=$row->comments;
          $html.=$this->load->view('blogs', $data, true); // returns view as data
    };
    echo html;
}

note: you can call your ajax function either on a click event or automatically every x seconds (e.g. with setTimeout() )

Upvotes: 3

Serg Chernata
Serg Chernata

Reputation: 12400

CodeIgniter (PHP) can not refresh a portion of a page. It can only refresh the entire page. As other commenters have mentioned, you can use AJAX to accomplish this. Try something like jQuery load method.

Basically, you will need to use ajax to save data to db AND update the contents on your page.

Upvotes: 0

user3654608
user3654608

Reputation: 78

CodeIgniter has a built-in method for detecting if the request is an AJAX request:

Try something like this:

// load the comments 

if ( $this->input->is_ajax_request() ) {
  // render a view that is just the comments
} else {
  // render a view that is the whole page
}

Upvotes: 1

adnan kamili
adnan kamili

Reputation: 9455

When you add new comments they get stored in the database, so when controller is invoked again it gets new data with more comments. As far as refreshing is concerned you should use ajax to invoke the controller and pass a variable to inform the controller that request is an ajax request and return json or xml data instead of html and update your view using client javascript by reading new data. The ajax request can be made to occur at repeated intervals using a timer.

Upvotes: 1

Related Questions