2014-03-27 84 views
1
<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> 

控制器:如何在控制器中使用ajax顯示codeigniter表?

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

回答

0

在你的控制器,你需要通過$this->table->generate($query);生成的表格echo回你的Ajax調用。您還需要將數組或數據庫結果對象傳遞給generate函數。所以,你需要在你的控制器在你的AJAX調用添加/修改下面的行

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

你的成功的功能,然後將捕捉到的數據遙相呼應從控制器回來,你可以做你想做的事情是什麼。

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 
} 
+0

我仍然無法獲取數據 – vini

+0

@vini查看更新代碼 – Pattle

相關問題