2017-07-31 24 views
0

我正在嘗試使用ajax刷新/重新載入頁面。Codeigniter中的Ajax問題

查看:

 <!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Insert | SP</title> 
     <script src="<?php echo base_url(); ?>bootstrap/js/jquery-2.1.1.min.js"></script> 

腳本部分

<script> 
     $(document).ready(function(){ 

      function loadNowPlaying(){ 
     $(this).load('<?php echo base_url().'Store/student' ;?>'); 
    } 
    setInterval(function(){loadNowPlaying()}, 5000); 
     }); 


     </script> 

重命名代碼

</head> 
    <body> 
     <form action="<?php echo base_url();?>Store/add" method="post"> 
      <input type="text" name="name" id="name"><br> 
      <input type="text" name="mail" id="mail"><br> 
      <input type="password" name="pass" id="pass"><br> 
      <input type="submit" name="submit" id="submit" value="Insert"> 
     </form> 


     <table id="studata"> 
      <?php foreach ($students as $student):?> 
      <tr> 
       <th>Name</th> 
       <th>Email</th> 
       <th>Password</th> 
      </tr> 
       <tr> 
       <td><?php echo $student->Name;?></td> 
       <td><?php echo $student->Email;?></td> 
       <td><?php echo $student->Password;?></td> 
      </tr> 
      <?php endforeach;?> 
     </table> 
    </body> 
    </html> 

我的控制器代碼:

public function student() 
    { 
     $data["students"] = $this->sp->getstudent(); 
     $this->load->view('student',$data); 
    } 

我的模型:

public function getstudent() 
    { 
     $this->db->select('*'); 
     $this->db->from('ex_sp'); 
     $query = $this->db->get(); 
     return $query->result(); 
    } 

的代碼工作正常,但它加載了我的整個頁面[查看],我想只加載表或者一些特定的容器,但使用相同的視圖。我檢查了很多問題,但沒有找到我想要的,請檢查問題而不是投下。 它有什麼問題?有人可以看看它。

+2

我不是在那裏看到任何Ajax。 – Difster

+0

你正試圖從視圖本身加載視圖從jQuery加載函數? –

+0

你的ajax代碼在哪裏? –

回答

3

在你的控制器和視圖一個isAjax標誌

在控制器:

public function student() 
{ 
     $data["isAjax"] = $_REQUEST['isAjax']; //change it to CI Get/Post method you are using. 
     $data["students"] = $this->sp->getstudent(); 
     $this->load->view('student',$data); //you are sending $data["isAjax"] to view file 
} 

現在你有如果設置了isAjax標誌,則檢查視圖,然後只加載表格,否則加載整個頁面:

在View:

if($isAjax == 1) 
    // Load data only needed for ajax request 
else 
    // Load all other data 

請注意:您將需要發送額外的PARAM使用Ajax請求作爲isAjax = 1:

Ajax代碼示例:

$.ajax({ 
    url: "view_file_ajax_call_url", 
    method: "POST", 
    data: { 'isAjax':1, 'example_data': value}, 
    }).done(function(data) { 
     //operation 
    } 
+0

可以請你解釋一下.. – msz

+1

在發送額外的參數與ajax請求isAjax = 1並獲得它在控制器。所以,如果它是ajax請求,您將在視圖中使用isAjax = 1。你有if條件的觀點,如果(isAjax == 1)「加載只有表」其他「加載所有」 – BSB

+0

仍然沒有得到它,你可以請你在我的代碼中添加代碼示例上面提供的代碼,所以我可以測試它。 – msz

0

在你的腳本部分試圖改變''""

<script> 
     $(document).ready(function(){ 

      function loadNowPlaying(){ 
     $(this).load("<?php echo base_url().'Store/student' ;?>"); 
    } 
    setInterval(function(){loadNowPlaying()}, 5000); 
     }); 


     </script>