我無法將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」的數據,無論我通過測試控制器發送了哪個值。
在阿賈克斯你沒有通過價值.. –