【问题标题】:Passing a variable into javascript, codeigniter将变量传递给javascript,codeigniter
【发布时间】:2015-10-14 06:56:13
【问题描述】:

我无法将 Codeigniter 视图中的变量传递给控制器​​,然后模型会拍摄从数据库中获取数据所需的查询。

这是我的控制器(sn-p):

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();
    }
}

我正在从另一个函数发送“$id”:

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 视图:

<?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,我正在使用 dataTables。:

在view_name中渲染的All.js(读取数据sn-p):

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" 的数据。

【问题讨论】:

  • 在 ajax 中你没有传递值..

标签: javascript php ajax codeigniter datatables


【解决方案1】:

将您的模型代码更改为:

 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”作为 id 的数据
【解决方案2】:

试试这个

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>

在您的控制器中尝试像这样获取 id

public function getAll($id)
{

  echo $id;
}

更多试试这个:http://w3code.in/2015/10/how-to-edit-delete-and-update-data-without-refreshing-page-in-codeigniter/

【讨论】:

  • 我仍然得到只有“0”作为 id 的数据
  • 我无法在控制器中回显,因为数据是由 datatables.js 获取的,并且它将继续加载,因为它不支持 echo $id;
  • 好吧试试这个w3code.in/2015/10/…一切都用数据表详细解释
  • 问题是,如果我不传递自己的$id,我可以获得所有数据,但我只想要具有特定 ID 的数据。
  • 为此,您需要将 id 保存在 getAll onclick 函数中,如下所示 onclick="getAll('your id goes here')"
【解决方案3】:

像这样在ajax中添加你想要的id到post,然后你可以在模型或控制器中使用$this-&gt;input-&gt;post("id");

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

    }
});

【讨论】:

  • 我仍然得到只有“0”作为 id 的数据
  • 在您的代码中data:{id: id which you want to pass.} 我尝试发送一个默认值,如data:{id: 2} 仍然没有运气
  • 你使用的是json,改成POST
  • 如果我将其更改为 POST,它会继续加载
  • post 应该可以工作,因为您正在从 post 中获取价值。只需刷新页面并重试。
猜你喜欢
  • 2011-06-29
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2012-01-28
相关资源
最近更新 更多