如何使用ajax在控制器视图中显示编码表


How to display codeigniter table in view from controller using ajax?

  <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{
}
}

请帮助我是新的编码器和php

在您的控制器中,您需要将$this->table->generate($query);生成的表echo返回给ajax调用。此外,您需要将数组或数据库结果对象传递给generate函数。因此,您需要在控制器

中添加/修改以下行
$table = $this->table->generate($query->result_array()); //Pass an array
echo $table; //Added this
然后,AJAX调用中的success函数将捕获从控制器回显的数据,您可以对它进行任何处理。
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
}