如何使用ajax从控制器中显示codeigniter表?

时间:2014-03-27 10:16:50

标签: php ajax codeigniter

  <div class="panel-body">
        <script type="text/javascript">
            $.ajax({
    type : 'POST',
    url : '<?php echo site_url('welcome/displayallquestionsandoptions')?>',
    data : '' // query string
    success : function(formagain){
        $('.panel-body').html(formagain);
        //this will replace the content of div with new form
    } 
});


        </script>


    </div>

控制器:

 public function displayallquestionsandoptions() {


     if($this->input->is_ajax_request()){
       $this->load->database();


    $this->load->library('table');

    $query = $this->db->query("SELECT QuestionId,Name,SurveyId,CreatedOn from question");

    $table = $this->table->generate($query); 



}else{

}

}

请帮助我是codeigniter和php的新手

1 个答案:

答案 0 :(得分:0)

在你的控制器中,你需要echo $this->table->generate($query);生成的表回到你的ajax调用。您还需要将数组或数据库结果对象传递给generate函数。因此,您需要在控制器中添加/修改以下行

$table = $this->table->generate($query->result_array()); //Pass an array
echo $table; //Added this

您在AJAX调用中的成功函数将捕获从控制器回送的数据,您可以使用它执行所需的操作。

success : function(formagain){ //foragain contains the table HTML from the controller
    $('.panel-body').html(formagain);
    //this will replace the content of div with new form
}