Php 使用codeigniter jquery填充下拉列表

Php 使用codeigniter jquery填充下拉列表,php,jquery,codeigniter,Php,Jquery,Codeigniter,我有一个从数据库填充的下拉列表。该州人口分布良好,但该市返回html 视图 <?php $this->load->view('header.php'); ?> <?php echo form_open('control_form/add_all'); ?> <label for="f_state">State<span class="red">*</span></lab

我有一个从数据库填充的下拉列表。该州人口分布良好,但该市返回html

视图

        <?php $this->load->view('header.php'); ?>

        <?php echo form_open('control_form/add_all'); ?>
        <label for="f_state">State<span class="red">*</span></label>
        <select id="f_state" name="f_state">
            <option value=""></option>
            <?php
            foreach($states as $state) {
     echo '<option value="' . $state->id . '">' . $state->statename .  '</option>';
            }
            ?>
        </select>
        <label for="f_city">City<span class="red">*</span></label>
        <!--this will be filled based on the tree selection above-->
        <select id="f_city" name="f_city" id="f_city_label">
            <option value=""></option>
        </select>
        <label for="f_membername">Member Name<span class="red">*</span></label>
        <!--<input type="text" name="f_membername"/>-->
        <?php echo form_close(); ?>


        <script type="text/javascript">

            $('#f_city, #f_city_label').hide();
            $('#f_state').change(function(){
                var state_id = $('#f_state').val();
                if (state_id != ""){
                    var post_url = "/index.php/control_form/get_cities/" + state_id;
                    $.ajax({
                        type: "POST",
                        url: post_url,
           success: function(cities) //we're calling the response json array 'cities'
                        {
                            $('#f_city').empty();
                            $('#f_city, #f_city_label').show();
                            $.each(cities,function(id,city)
                            {
   var opt = $('<option />'); // here we're creating a new select option for each group
                                opt.val(id);
                                opt.text(city);
                                $('#f_city').append(opt);
                            });
                        } //end success
                    }); //end AJAX
                } else {
                    $('#f_city').empty();
                    $('#f_city, #f_city_label').hide();
                }//end if
            }); //end change

        </script>
控制器

    Class Control_form extends CI_controller
    {
       function add_all(){
            $this->load->library('form_validation');
            #Validate entry form information
            $this->load->model('Model_form','', TRUE);
            $this->form_validation->set_rules('f_state', 'State', 'required');
            $this->form_validation->set_rules('f_city', 'City', 'required');
            $this->form_validation->set_rules('f_membername', 'Member Name', 'required');

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

            if ($this->form_validation->run() == FALSE)
            {
                  $this->load->view('view_form_all', $data);
            }
            else
            {
                #Add Member to Database
                $this->Model_form->add_all();
                $this->load->view('view_form_success');
            }
        }

         function get_cities($state){
            $this->load->model('Model_form');
           // header('Content-Type: application/x-json; charset=utf-8');
            echo json_encode($this->Model_form->get_cities_by_state($state));
        }

    }

以下代码不正确:

opt.val(id);
这只是用于id,而不是设置它。请尝试以下方法:

opt.attr('id', id);
这可能是问题的一部分,如果不是全部的话。

尝试以下方法:

改变

     function get_cities_by_state ($state)

并将base_url()添加到url:

url:“控制表单/获取城市/”+州id,
确保在标题中包含jquery库

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


HTML是否来自PHP错误,可能是由
$query=$this->db->get('cityy')引起的?查询返回正确的结果。使用var_dump检查它可能是
$。each()
无法处理您返回的JSON?请尝试从PHP脚本复制JSON编码的输出,然后在其上运行$。每个函数,并查看控制台中是否有任何错误。你可以把
die()echo json_encode(…
后进行代码>以获取输出。
    function get_cities_by_state($state = null){

     $this->db->select('id, city');

     if($state != NULL){
     $this->db->where('stateid', $state);
      }

     $query = $this->db->get('cityy');

     $cities = array();

     if($query->result()){
     foreach ($query->result() as $city) {
     $cities[$city->id] = $city->city;
     }
     return $cities;
     }else{
      return FALSE;
     }
    }
   url: "<?php echo site_url(); ?>control_form/get_cities/"+state_id,
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>