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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算