javascript - 如何在 Codeigniter 中使用 Ajax 将变量从 Controller 传递到 View ?

标签 javascript php jquery ajax codeigniter

我在将数据从 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);
        })          
    });
});

我会得到 id_thang单击 HTML 标记时 li > thang .

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 show all data I selected。

在 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到这个 View 。

更新 1:

我试着输入:console.log(result);.done(function(result)事件和接收结果如下:

问题是:我使用 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;
}

在 Controller 中,我调用 get_info .注意:

Mmenushoatnao是数据库中的模型图。

更新 3:

我只知道在Controller写代码在click之后获取数据事件。

但是就像你提到的问题一样。必须用 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);
 })

在此之前,在您要显示此结果的 View 页面中提供任何 ID

<div id="res" class="col-md-4 col-sm-4 col-xs-4">

</div>

关于javascript - 如何在 Codeigniter 中使用 Ajax 将变量从 Controller 传递到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010944/

相关文章:

javascript - $(element).data ("dataAtribute") 与 $(element).hasClass ("className")

php - SQL - 调用其他SQL文件创建多个数据表

php - 这是进行实时聊天的最有效方式吗?

php - 使用ajax将post数据提交到php文件

JavaScript 函数回调

javascript - 仅拆分和组合字符串的一部分

PHP mktime 基于分钟计算的不同结果

javascript - 为每个 <div> 独立运行一个 Jquery 脚本

javascript - 带有 localStorage 的 jQuery 图片 uploader 。部分工作。默认图片不显示

javascript - 在 Javascript 中,属性如何修改对象