2015-10-14 76 views
-1

我無法將Codeigniter視圖中的變量傳遞給控制器​​,然後使用模型來拍攝從數據庫中獲取數據所需的查詢。將變量傳遞給javascript,codeigniter

這裏是我的控制器(片段):

public function read() { 
     echo json_encode($this->random_model->getAll()); 
} 

型號:

public function getAll() { 

    $id = $this->input->post('id'); 
      $id = intval($id); 
    // print_r($sg_id); 

    // var_dump($sg_id); 


    $query = $this->db->where('id',$id)->limit(100)->get('table_name'); 



    if($query->num_rows() > 0) { 
     return $query->result(); 
    } else { 
     return array(); 
    } 
} 

我從另一個函數發送 「$標識」:

function test1() 
{ 

    $blaa['id'] = $this->input->post('id'); 

    if ($query = $this->model_name->getAll($blaa)) 
    { 
     $blaa['records'] = $query; 
    } 

    //$this->model_name->getAll($blaa); 
    $this->load->view('view_name', $blaa); 

} 

這是test1 view:

<?php $attributes = array("class" => "", "id" => "", "name" => ""); 

    echo form_open("test/test1/", $attributes);?> 

<input type="text" id="id" name="id" > 

<?php echo form_submit('submit', 'Submit')?> 

    <?php echo form_close(); ?> 

這是顯示所有數據VIEW_NAME,我使用的數據表:

All.js是在VIEW_NAME呈現(讀取數據片段):

var readUrl = 'index.php/controller_name/read' 

function readUsers() { 
//display ajax loader animation 
$('#ajaxLoadAni').fadeIn('slow'); 

$.ajax({ 
    url: readUrl, 
    dataType: 'json', 
    success: function(response) { 

     for(var i in response) { 
      response[ i ].updateLink = updateUrl + '/' + response[ i ].id; 
      response[ i ].deleteLink = delUrl + '/' + response[ i ].id; 
     } 

     //clear old rows 
     $('#records > tbody').html(''); 

     //append new rows 
     $('#readTemplate').render(response).appendTo("#records > tbody"); 

     //apply dataTable to #records table and save its object in dataTable variable 
     if(typeof dataTable == 'undefined') 
      dataTable = $('#records').dataTable({"bJQueryUI": true}); 

     //hide ajax loader animation here... 
     $('#ajaxLoadAni').fadeOut('slow'); 
    } 
}); 

的結果表明,我得到的僅僅是ID爲「0」的數據,無論我通過測試控制器發送了哪個值。

+0

在阿賈克斯你沒有通過價值.. –

回答

0

添加你想要的ID postajax這樣,那麼你可以在模型或控制器中使用$this->input->post("id");

$.ajax({ 
    url: readUrl, 
    data:{id: id which you want to pass.} 
    dataType: 'json',// change this to post 
    success: function(response) { 

    } 
}); 
+0

我仍然得到的數據,只有「0」作爲編號 – Dray

+0

編輯您的代碼,並告訴我們如何通過在您的代碼ID .. –

+0

數據: {我想嘗試發送一個默認值,例如'data:{id:2}'仍然沒有運氣 – Dray

0

更改模型的代碼如下:

public function getAll($id = null) { 
    if ($id == null) 
     $id = $this->input->post('id'); 
    $id = intval($id); 
    // print_r($sg_id); 

    // var_dump($sg_id); 


    $query = $this->db->where('id',$id)->limit(100)->get('table_name'); 



    if($query->num_rows() > 0) { 
     return $query->result(); 
    } else { 
     return array(); 
    } 
} 
+0

我仍然得到數據只有「0」作爲編號 – Dray

0

試試這個

function getAll(id) 
    { 

     var id = id; 

     $.ajax({ 
      url:"<?php echo site_url('controllerName/getAll');?>/"+id, 
      success:function(data) 
      { 

      alert(data); 

      } 

      }); 
     } 


<a href="javascript:void(0)" onclick="getAll(1)" title="Delete" class="icon-2 info-tooltip">Get All</a> 

在你的控制器試試這樣

public function getAll($id) 
{ 

    echo $id; 
} 

更多的嘗試這種獲取ID :http://w3code.in/2015/10/how-to-edit-delete-and-update-data-without-refreshing-page-in-codeigniter/

+0

我仍然得到的數據只有「0」作爲編號 – Dray

+0

好吧我已經uupdated我的答案再試試這個 – Ricky

+0

我無法在控制器中回顯,因爲數據是由datatables.js,它將繼續加載,因爲'echo $ id;'不被它支持。 – Dray