2015-10-14 75 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