dongliao1949 2015-10-14 06:56
浏览 49

将变量传递给javascript,codeigniter

I am not able to pass a variable from Codeigniter's view to controller and then model that shoots the query that is required to fetch the data from database.

here's my Controller(snippet):

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

Model:

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

I'm sending the "$id" from another function :

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

}

this is the 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(); ?>

this is the view_name that shows all the data, I'm using dataTables.:

All.js that is rendered in the view_name (read data snippet):

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

The results that I get are only of the data having the id "0" , no matter which value I send through the test controller.

  • 写回答

3条回答 默认 最新

  • dongmi4809 2015-10-14 07:09
    关注

    add the id you want to post in ajax like this, Then you can use $this->input->post("id"); in model or controller.

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

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败