javascript – 如何在Codeigniter中使用Ajax将变量从Controller传递到View?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何在Codeigniter中使用Ajax将变量从Controller传递到View?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3032字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 如何在Codeigniter中使用Ajax将变量从Controller传递到View?](/upload/InfoBanner/zyjiaocheng/796/1abe6088844f42a487e7d1f92b718f4a.jpg)
我将数据从Controller传递到View时遇到问题.
我使用Ajax来执行此操作,您可以在此处查看我的Ajax代码:
$(document).ready(function(){
$('li.thang').click(function(){
var id_thang = $(this).attr('value');
$.ajax({
url: baseUrl+'/Home/getImage',
dataType: 'json',
type: 'POST',
data: {id_thang: id_thang},
}).done(function(result) {
console.log(result.get_list_image_thang);
})
});
});
点击HTML标签时我会得到id_thang li>胜.
在Controller / Home.php中,我将获得有关此id_thang的数组数据.
function getImage(){
$id_thang = $this->input->post('id_thang');
$input = array();
$input['order'] = array('id','ASC');
$get_image_thang = $this->Mmenushoatnao->get_info($id_thang);
ob_start();
}
所有数据都存储在数组$get_image_thang中.
最后,我不知道如何将此数组传递给View显示我选择的所有数据.
在View / index.php中,我正在尝试使用此数组中的所有数据进行foreach循环,并在< html>中显示标签.像这样:
<?php foreach($get_image_thang AS $item) ?>
<div class="col-md-4 col-sm-4 col-xs-4">
<?php echo $item->id; ?>
</div>
<?php endforeach ?>
注意:在View / index.php是演示代码.
问题是我不知道如何将$get_image_thang发送到此视图.
更新1:
我试着把:console.log(结果);到.done(函数(结果)事件并收到如下结果:
问题是:我使用row = result [i] .id;或任何属性,如id,name,image_list未定义.
更新2:
只有两个函数来获取id的信息.我在core / MY_MODEL.php中编写了所有代码:
function get_info($id, $field = '')
{
if (!$id)
{
return FALSE;
}
$where = array();
$where[$this->key] = $id;
return $this->get_info_rule($where, $field);
}
function get_info_rule($where = array(), $field= '')
{
if($field)
{
$this->db->select($field);
}
$this->db->where($where);
$query = $this->db->get($this->table);
if ($query->num_rows())
{
return $query->row();
}
return FALSE;
}
在控制器,我调用get_info.注意:
Mmenushoatnao是数据库中的模型映射.
更新3:
我只知道在Controller中编写代码以获取点击事件后的数据.
但就像你提出的问题一样.必须用Ajax代码编写所有代码.
像这样:
function getImage(){
$id_thang = $this->input->post('id_thang');
$input = array();
$input['order'] = array('id','ASC');
$get_image_thang = $this->Mmenushoatnao->get_info($id_thang);
ob_start();
?>
<?php foreach($get_image_thang as $item): ?>
<?php $image_list = json_decode($item->image_list); ?>
<?php foreach($image_list as $image): ?>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="<?php echo upload_url() ?>/img/hoatnao/hinhanh/<?php echo $image ?>" alt="Image" class="img-responsive">
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<?php
$return = ob_get_clean();
$data['result']=$return;
echo json_encode($data);
}
当然,这段代码不起作用.
所以,我们需要转换为Ajax代码.
解决方法:
试试这个
function getImage(){
$id_thang = $this->input->post('id_thang');
$input = array();
$input['order'] = array('id','ASC');
$get_image_thang = $this->Mmenushoatnao->get_info($id_thang);
echo json_encode($get_image_thang);
}
现在在ajax中(假设你从get_info()方法返回对象)
//....
.done(function(result) {
var row = '';
for (var i = 0; i < Object.keys(result).length; i++) {
row += result[i].id;
}
$('#res').html(row);
})
在它之前,在视图页面中提供要显示此结果的任何ID
<div id="res" class="col-md-4 col-sm-4 col-xs-4">
</div>
内容总结
以上是互联网集市为您收集整理的javascript – 如何在Codeigniter中使用Ajax将变量从Controller传递到View?全部内容,希望文章能够帮你解决javascript – 如何在Codeigniter中使用Ajax将变量从Controller传递到View?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。