依赖于Codeigniter的下拉列表,包含ajax和Jquery

时间:2015-12-13 19:07:04

标签: jquery ajax codeigniter dropdown

我的数据库中有2个表:DepartmentsServices

每个部门都有很多服务,所以当我从第一个下拉列表中选择一个部门时,我希望我的第二个下拉列表中包含相应的服务。

问题在于,当我点击列表中的某个部门时,我得到了这个 控制台:

  

POST http://localhost/ci/control_form/get_services/24 500(内部   服务器错误)

我当时认为我提供的post_url可能存在问题,所以我该如何编写它以便我不会收到该错误?

另外,我的服务响应数组没有被发送到任何地方的视图,所以我怎么能这样做?这是必要的吗?

我在我的视图中有这个代码test_form.php

<?php echo form_open('control_form/add_all'); ?>
<label for="f_dep">Departament<span class="red">*</span></label> // first dropdown
    <select id="f_dep" name="f_dep">
        <option value=""></option>
        <?php
        foreach($deps as $d){
            echo '<option value="' . $d->id_departament . '">' . $d->Nume_dep . '</option>';
        }
        ?>
    </select>
<label for="f_serviciu">Serviciu<span class="red">*</span></label>  //second dropdown
    <select id="f_serviciu" name="f_serviciu" id="f_serviciu_label"> 
        <option value=""></option>
    </select>  

这是Jquery函数:

$('#f_serviciu, #f_serviciu_label').hide();
$('#f_dep').change(function(){
var departament_id = $('#f_dep').val();
if (departament_id != ""){
    var post_url =  "<?= base_url() ?>control_form/get_services/" + departament_id;
    $.ajax({
        type: "POST",
         url: post_url,
         success: function(servicii) //we're calling the response json array 'cities'
          {
            $('#f_serviciu').empty();
            $('#f_serviciu, #f_serviciu_label').show();
               $.each(servicii,function(id,serviciu) 
               {
                var opt = $('<option />'); // here we're creating a new select option for each group
                  opt.val(id);
                  opt.text(serviciu);
                  $('#f_serviciu').append(opt); 
            });
           } //end success
     }); //end AJAX
} else {
    $('#f_serviciu').empty();
    $('#f_serviciu, #f_serviciu_label').hide();
}//end if
}); //end change 

这是我的控制器中的get_services函数(control_form.php):

    function get_services($departament){
    $this->load->model('Model_form','', TRUE);    
    header('Content-Type: application/x-json; charset=utf-8');
    $data = $this->Model_form->get_servicii_by_departament($departament);
            echo(json_encode($data));
} 

我的control_form.php有以下方法:

public function index() {
    $data['deps'] = $this->model_departament->get_all_departments(); 
    $this->load->view('test_form', $data);
}

function add_all(){
    #Validate entry form information
    $msg="";
    $this->load->model('Model_form','', TRUE);
    $this->form_validation->set_rules('f_dep', 'Departament', 'required');
    $this->form_validation->set_rules('f_serviciu', 'Serviciu', 'required');
    if ($this->form_validation->run() == FALSE) {        
        $msg = "error";
        echo $msg;
    }
    else {      
        $this->load->view('successful_employee');
    }
} 

function get_services($departament){
    $this->load->model('Model_form','', TRUE);    
    header('Content-Type: application/x-json; charset=utf-8');
    $data = $this->Model_form->get_servicii_by_departament($departament);
        echo(json_encode($data));
}   

Model_form.php:

 function get_departament(){
    $query = $this->db->query('SELECT id_departament, Nume_dep FROM departament');
    return $query->result();
}

function get_servicii_by_departament ($departament){
    $sql = 'select S.Nume_serviciu from serviciu S, departament D where 
    S.Id_dep=D.id_departament and D.id_departament ='. '$departament';
    $query = $this->db->query($sql);
    $servicii = array();
    if($query->result()){
        foreach ($query->result() as $serviciu) {
            $servicii[$serviciu->Id_serviciu] = $serviciu->Nume_serviciu;
        }
        return $servicii;
    } else {
        return FALSE;
    }
} 

1 个答案:

答案 0 :(得分:0)

检查此网址: http://www.91weblessons.com/codeigniter-ajax-country-state-city-drop-down/

并按部门和服务替换州和城市