未定义的变量:ci

时间:2017-06-22 09:58:21

标签: jquery codeigniter

控制器:test.php的

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Test extends CI_Controller 
    {
        function __construct() 
        {
            parent :: __construct();
            $this->load->helper(array('form', 'url'));
            $this->load->model('Fetch_data');
        }
        public function get_stream()
        {
            $fields = $this->input->post('fields');
            $data['fields'] = $this->Fetch_data->stream_exam($fields);
            $this->load->view('header',$data);
        }
    }

查看:header.php

<script>
    $(document).ready(function(){
        $(".stream").click(function(){
            streamid = this.id;
            alert(streamid);
            fields = $("#fields").val(streamid);
            $.ajax({
                type:"POST",
                data:{"fields":fields},
                url:"<?php echo base_url('index.php/'); ?>test/get_stream",
                success:function(data){
                    alert(data);
                }
            }); 
        });
    });
</script>
<a href="#" id="other" data-toggle="modal" data-target="#myModal">Others</a>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel" style="text-align: center;color: #db333c;">Search More Streams Results</h4>
            </div>
            <div class="modal-body">
                <div role="tabpanel">
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active">
                            <a href="#agriculture" aria-controls="uploadTab" role="tab" data-toggle="tab" class="stream" id="agriculture">Agriculture</a>
                        </li>
                        <li role="presentation">
                            <a href="#architecture" aria-controls="browseTab" role="tab" data-toggle="tab" class="stream" id="architecture">Architecture</a>
                        </li>
                        <li role="presentation">
                            <a href="#commerce" aria-controls="browseTab" role="tab" data-toggle="tab" class="stream" id="commerce">Commerce</a>
                        </li>
                    </ul>
                    <input type="hidden" name="fields" id="fields"/>
                    <?php
                        foreach ($fields as $fetch) 
                        {
                    ?>
                            <div class="tab-content">
                                <div role="tabpanel" class="tab-pane active" id="<?php echo $fetch['field']; ?>">
                                    <div class="col-md-3">
                                        <p id="p">Top Exams of <?php echo $fetch['field']; ?></p>
                                        <div id="exams"></div>
                                    </div>
                                </div>
                            </div>
                    <?php        
                        }
                    ?>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

型号:Fetch_data.php

<?php  
    class Fetch_data extends CI_Model  
    {  
        function __construct()  
        {   
            parent::__construct();  
        }  
        public function stream_exam($fields)
        {
             $this->db->select('exam_name');
             $this->db->from('all_exams_details');
             $this->db->where('field',$fields);
             $query = $this->db->get();
             $result = $query->result_array();
             return $result;
        }
    } 

enter image description here

我是codeigniter的新手。在我的视图中,我有一个模态,当点击其他链接时,一个模态将打开后我有一个类(流)并且当我点击流选项卡时它的ID也将值传递给输入字段名称(字段) jquery / ajax我创建了一个控制器名称Test.php,其中我将输入字段的值发布到通过控制器查询。但没有结果表明我不知道问题出在哪里。那么,我该如何解决呢?请帮忙。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试如下,或者您可以将json返回到ajax成功,然后使用json数据设置值

public function test()
{
    $fields = $this->input->post('fields');
    $data['fields'] = $this->Fetch_data->stream_exam($fields);
    if(!empty($data['fields']))
    {
        foreach ($data['fields'] as $fetch) 
        {
        ?>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="<?php echo $fetch['field']; ?>">
                    <div class="col-md-3">
                        <p id="p">Top Exams of <?php echo $fetch['field']; ?></p>
                        <div id="exams"></div>
                    </div>
                </div>
            </div>
        <?php        
        }
    }
}

<script>
    $(document).ready(function(){
        $(".stream").click(function(){
            streamid = this.id;
            alert(streamid);
            fields = $("#fields").val(streamid);
            $.ajax({
                type:"POST",
                data:{"fields":fields},
                url:"<?php echo base_url('index.php/'); ?>test/test",
                success:function(data){
                    $('#refresh').html(data);
                }
            }); 
        });
    });
</script>

在视图中

<div id='refresh'>

</div>