dongluo1853
2014-05-20 19:16
浏览 60
已采纳

Codeigniter - 使用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 :/

图片转代码服务由CSDN问答提供 功能建议

我不知道如何使用jquery刷新发送到视图的数据而不刷新整个网站。 我写了这样的东西:

控制器:

 公共功能博客($ id){
 
 $ data ['comments  '] = $ this-&gt;评论 - &gt; findall('blog',$ id); 
 $ this-&gt; load-&gt; view('sites / blog',$ data); 
 
}  
   
 
 

型号:

...从数据库中读取......

查看:

 &lt; div id =“comments”&gt; 
 
读取控制器博客发送的$ comments中的数据
 
&lt; / div&gt; 
   
 
 

如何在控制器BLOG中的函数读取新刷新的注释数量的视图中使用jQuery刷新DIV注释? 如果我输入DIV注释代码:

 &lt;?  $这 - &GT;负载&gt;查看( '位点/评论');  ?&gt; 
   
 
 

使用jQuery刷新此DIV这是不成功的,因为控制器发送的数据保持不变:/

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dougu3290 2014-05-20 22:55
    已采纳

    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() )

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshi7433 2014-05-20 19:25

    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.

    评论
    解决 无用
    打赏 举报
  • dongtao9887 2014-05-20 20:02

    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
    }
    
    评论
    解决 无用
    打赏 举报
  • dongzao9044 2014-05-20 21:54

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题