【发布时间】: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