[英]Passing a variable into 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. 我无法将变量从Codeigniter的视图传递到控制器,然后传递捕获从数据库中获取数据所需的查询的模型。
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 : 我从另一个函数发送“ $ 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);
}
this is the test1 view: 这是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(); ?>
this is the view_name that shows all the data, I'm using dataTables.: 这是显示所有数据的view_name,我正在使用dataTables。
All.js that is rendered in the view_name (read data snippet): 在view_name中呈现的All.js(读取数据片段):
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. 无论我通过测试控制器发送哪个值,我得到的结果仅是ID为“ 0”的数据。
add the id you want to post
in ajax
like this, Then you can use $this->input->post("id");
像这样在
ajax
添加要post
的id,然后可以使用$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 ) {
}
});
Change your model code to this : 将您的模型代码更改为此:
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();
}
}
Try this 尝试这个
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>
In your controller try to fetch id like this 在您的控制器中尝试像这样获取id
public function getAll($id)
{
echo $id;
}
for more try this : http://w3code.in/2015/10/how-to-edit-delete-and-update-data-without-refreshing-page-in-codeigniter/ 有关更多信息,请尝试: http : //w3code.in/2015/10/how-to-edit-delete-and-update-data-without-refreshing-page-in-codeigniter/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.