【问题标题】:How can I pass input value from view to controller and model to fetch data correctly? Codeigniter如何将输入值从视图传递到控制器和模型以正确获取数据?代码点火器
【发布时间】:2020-03-16 14:34:21
【问题描述】:

我正在尝试获取与我的输入隐藏值具有相同 id 的数据。

我无法获取数据的 id 或输入隐藏值

首先,这是为了让我更清楚地调试我所做的事情,我制作了一个硬编码表单和一个按钮来将我带到页面

在视图中 - groups/index.php

如您所见,我创建了 posts/index/1,这是一个硬编码值,但我可以稍后轻松更改,这不是我的问题

<?php echo form_open('posts/index/1') ?>

    <input type="hidden" name="txtGroup" value="1" />
    <button type="submit" class="btn btn-info"> Submit</button>

</form>

所以在我制作完表单后,我将在控制器中创建一个函数来获取posts/index

在控制器中 - Posts.php

public function index(){
    $this->load->view('templates/header');
    $this->load->view('teachers/posts/index');
    $this->load->view('templates/footer');   
}

于是我获取了页面。至此,我现在可以通过/posts/index/1 看到我的页面

在我的页面 posts/index.php 我有一个数据,它是通过 Ajax 获取的,这里是

所以我已经在posts/showPosts 中获取了这些数据

showAllQuestions();

//Show all data
function showAllQuestions(){
    $.ajax({
        type: 'ajax',
        url: '<?php echo base_url() ?>posts/showPosts',
        async: false,
        dataType: 'json',
        success: function(data){
            var html = '';
            var i;
            var n=1;

            for(i=0; i<data.length; i++){
                html +='<div class="card">'+
                    '<div class="card-header" style="color:white; background-color:black">'+
                        '<h4><span class="iconify" data-icon="ant-design:info-circle-outlined" data-inline="false"></span> Question No. '+ n++ +'</h4>'+
                    '</div>'+
                    '<div class="card-body">'+
                        '<form>'+
                            '<div class="form-group">'+
                                '<label for="exampleFormControlTextarea1"><h5> <span class="iconify" data-icon="emojione:check-mark-button" data-inline="false"></span> </h5></label>'+
                                '<input type="text" value="'+data[i].question+'" class="form-control" disabled />'+
                            '</div>'+
                            '<hr>'+
                            '<a href="<?php echo base_url() ?>posts/edit/'+data[i].id+'" class="btn btn-info"><span class="iconify" data-icon="el:edit" data-inline="false"></span> </a>&nbsp;'+
                            '<a href="<?php echo base_url()?>posts/delete/'+data[i].id+'" class="btn btn-primary"><span class="iconify" data-icon="fa-solid:trash-alt" data-inline="false"></span></a>'+
                            //  '<a href="javascript:;" class="btn btn-info item-edit" data-id="'+data[i].id+'">Edit </a>&nbsp;'+
                        '</form>'+
                    '</div>'+
                '</div><br>';
            }

            $('#showdata').html(html);
        },
        error: function(){
            alert('Could not get Data from Database');
        }
});

}

posts/showPosts - Posts.php,这是控制器

public function showPosts(){
    $result = $this->post_model->showPosts();
    echo json_encode($result);
}

最后是模型来确定我是否根据我提交的数据 id 获取了正确的 ID

问题是 $id 为空,我不知道如何开始,因为我在视图页面上声明了一个隐藏的输入值。

public function showPosts(){
    // Show questions and answers
    $id = $this->input->post('txtGroup');
    $this->db->select('*');
    $this->db->from('questions');
    $this->db->where('group_id', $id);
    $query = $this->db->get();
    return $result = $query->result_array();

}

【问题讨论】:

    标签: php ajax codeigniter codeigniter-3


    【解决方案1】:

    我将尝试比您之前的问题更好地理解这个问题(现在可以安全删除,因为这是对您问题的更好描述)。

    在您的 groups/index.php 视图中,您希望允许用户导航posts/index 页面并将整数作为单个参数传递(由您硬编码,未输入由用户)——我将其称为$txtGroup 以获取上下文。因为您没有执行INSERT|UPDATE|DELETE 操作,所以更好的做法是将数据作为$_GET 而不是$_POST 发送。因为 Codeigniter 允许将 $_GET 参数作为 url 的斜杠分隔扩展名传递,所以我认为设置 &lt;form&gt; 没有任何好处。

    使用您喜欢的类将您的新超链接设置为按钮。

    <a href="<?php echo base_url("posts/index/{$integer}"); ?>">Link to <?php echo $integer; ?></a>
    

    这会将您的数据发送到posts/index.php 控制器。这是您应该从 url 访问/提取 $txtGroup 值的地方。

    这就是我开始对您需要什么感到困惑的地方。如果您想将值传递给teachers/posts/index 视图,请在加载视图时将关联数组作为第二个参数传递。

    public function index($txtGroup) {
        $this->load->view('templates/header');
        // if you want to pass the value to THIS 
        $this->load->view('teachers/posts/index', ['txtGroup' => $txtGroup]);
        $this->load->view('templates/footer');   
    }
    

    如果您对将$txtGroup 传递给视图不感兴趣,那么将值从groups/index.php 传递给posts/index 的唯一其他原因是修改查询,然后将动态数据传递给@987654339 @view(无论如何在加载视图时都需要提供)。

    public function index($txtGroup) {
        $data['posts'] = $this->post_model->showPosts($txtGroup);
        $this->load->view('templates/header');
        $this->load->view('teachers/posts/index', $data);
        $this->load->view('templates/footer');   
    }
    

    在您的模型中,使用传递的参数。

    public function showPosts($groupId) {
        return $this->db->get_where('questions', ['group_id' => $groupId])->result_array();
    }
    

    这样,您不需要使用 ajax 调用(看起来无论如何您都在无条件地触发您的视图)。现在 Codeigniter 将发挥它的魔力,将$data 转移到您的视图中,您可以通过$posts 访问多维结果集(该变量由您分配的第一级键命名)。使用 foreach() 循环来迭代结果行并生成您的 html 内容——所有这些都使用服务器端语言。

    <?php foreach ($posts as $index => $post) { ?> 
        <div class="card">
        // ... the rest of your content ...
    <?php } ?>
    

    当您使用上述循环设计新的动态内容时:

    • 使用$index 作为您的计数器,这样您就不必手动递增。
    • 请勿允许INSERT|UPDATE|DELETE 操作由$_GET 事件触发,这不是最佳做法。这些类型的操作只能由$_POST 提交发起。

    【讨论】:

    • @Mana 你走了吗?
    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多