0

我很痛苦,但仍然无法在 Codeigniter 中创建一个依赖下拉框。这是架构:

groups(group_id,group)
forum(forum_id,subject)
group_forum(group_id,forum_id)

这是我的代码: 模型

function get_group(){

    $query = $this->db->get('group');
    return $query->result();
}

function get_subject_by_group($id)
{


        $subjects=array();

        $this->db->from('forum');
        $this->db->join('group_forum','group_forum.forum_id=forum.forum_id','group_id='.$id);
        $q=$this->db->get();
       foreach($q->result() as $y)
       {
           $subjects[$y['forum_id']] = $y['subject'];
       }


       return $subjects;

    }
}

控制器:

   <?php
class  C_control_form extends  CI_Controller {

     function add_all(){

        #Validate entry form information
        $this->load->model('Model_form');
        $this->form_validation->set_rules('f_group', 'Group', 'trim|required');
       $this->form_validation->set_rules('f_forum', 'Forum', 'trim|required');


        $data['groups'] = $this->Model_form->get_group(); //gets the available groups for the dropdown

        if ($this->form_validation->run() == FALSE)
        {

              $this->load->view('header_for_combo',$data);
              $this->load->view('view_form_all', $data);
              $this->load->view('footer_for_combo',$data);
        }
        else
        {
            #Add Member to Database
            $this->Model_form->add_all();
            $this->load->view('view_form_success');
        }
    }

 function get_subjects($group)
  {
       //echo "hi";
        $this->load->model('Model_form');
        header('Content-Type: application/x-json; charset=utf-8');
        echo (json_encode($this->Model_form->get_subject_by_group($group)));

    }



}
?>

看法

<html>
<head>
    <script type="text/javascript" src="<?php echo base_url("js/jquery-1.7.2.min.js"); ?>"  ></script>
    <script type="text/javascript">

    // $('#f_group, #f_forum').hide();
      $(document).ready(function(){
        $('#f_group').change(function(){

            var group_id = $('#f_group').val();
            if (group_id != ""){


              var post_url = "index.php/c_control_form/get_subjects/"+group_id;
              //    var post_url = "<?php echo base_url();?>"+"c_control_form/get_subjects"+group_id;

                $.ajax({
                    type: 'POST',
                    url: post_url,
                    dataType : 'json',

                    success: function(subjects) //we're calling the response json array 'cities'
                    {
                        $("#f_forum > option").remove();

                      //  $('#f_forum').empty();
                      //  $('#f_forum, #f_forum_label').show();

                        $.each(subjects,function(forum_id,subject)
                        {
                            var opt = $('<option/>'); // here we're creating a new select option for each group
                            opt.val(forum_id);
                            //alert(id);
                            opt.text(subject);
                            $('#f_forum').append(opt);
                        });
                    } //end success
                }); //end AJAX
            } else {
                $('#f_forum').empty();
            //   $('#f_forum, #f_forum_label').hide();
            }//end if

        }); //end change

        });
    </script>

</head>
<body>
<?php echo form_open('c_control_form/add_all'); ?>
    <p>
    <label for="f_group">Group<span class="red">*</span></label>

    <select id="f_group" name="f_group">
        <option value=""></option>
        <?php
        foreach($groups as $group){
            echo '<option value="' . $group->group_id . '">' . $group->group_name.'</option>';
        }
        ?>
    </select>
    </p>

    <p>
    <label for="f_forum">Subject<span class="red">*</span></label>


    <select id="f_forum" name="f_forum" id="f_forum_label">
        <option value=""></option>
    </select>
    </p>
  <?php echo form_close(); ?>  
</body>

4

1 回答 1

0

请更换

$subjects[$y['forum_id']] = $y['subject'];

$subjects[$y->forum_id] = $y->subject;

在模型文件中。在 php 标记之后和之前的模型 php 文件中还有空格或换行符。

编辑:在下面添加

function(subjects){
    var select = $('#f_forum').empty();
    $.each(subjects.values, function(i,item) {
        select.append( '<option value="'
                             + item.id
                             + '">'
                             + item.name
                             + '</option>' ); 
    });
于 2012-07-17T05:59:35.380 回答