动态相关下拉列表code igniter上的国家和州

fcg9iug3  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(391)

我在工作的项目,使用一个动态或相关的下拉列表codeigniter。这是我的noob逻辑,视图中的国家值传递给模型,然后模型从视图中获取具有相同国家值的状态数据,然后控制器从该模型中获取状态数据并再次传递给视图。问题是状态下拉列表什么都不显示。对不起,我是新来的。

//This is the Controllers (Pkl.php)
$data['all_country'] = $this->Server_Model->get_country_model();
$data['all_state'] = $this->Server_Model->get_state_model();
$this->load->view('contents/page_dashboard', $data);

//This is the Models (Server_Model.php)
function get_country_model(){
            $db_jarlap = $this->load->database('gis_bali', TRUE);
            $db_jarlap->select("*");
            $db_jarlap->from("country");
            $que = $db_jarlap->get();
            return $que->result();
        }
function get_state_model(){
            $kakakoko = filter_input(INPUT_POST, 'country_id');
            $db_jarlap = $this->load->database('gis_bali', TRUE);
            $db_jarlap->select("*");
            $db_jarlap->from("state");
            $db_jarlap->where(" id_country = $kakakoko ");
            $que = $db_jarlap->get();
            return $que->result();
        }

//This is the Views Content (page_dashboard.php)
<script src="<?php echo base_url() ?>resources/js/jquery-3.3.1.min.js"</script>
<script>
            $(document).ready(function(){
            $('#formCountry').change(function(){
                  var country_id = $(this).val();

                  $.ajax({
                            url: "<?php echo base_url() ?            >application/models/Server_Model.php",
                        method: "POST", 
                        data: {country_id:country_id}, 
                        success: function(data) { 
                        $('#formState').html(data);
                                            }
                                    });
                                });
                            });
</script>

<select name="formCountry" id="formCountry">
<?php foreach($all_country as $semua_country): ?>
<option value="<?php echo $semua_country->id_country; ?>"><?php echo     $semua_country->nama_country; ?></option>
<?php endforeach; ?>
</select>

<select name="formState" id="formState" >
<?php foreach($all_state as $semua_state): ?>
<option value="<?php echo $semua_state->id_state; ?>"><?php echo $semua_state->nama_state; ?></option>
<?php endforeach; ?>
</select>

谢谢你的帮助。

c90pui9n

c90pui9n1#

您不应该直接调用model,而应该调用 Pkl 控制员,我假设你用的是 index() 上的方法 Pkl 控制器:

$(document).ready(function () {
    $('#formCountry').change(function () {
        var country_id = $(this).val();

        $.ajax({
            url: "<?php echo base_url() ?>pkl", // using Pkl controller
            method: "POST",
            data: {
                country_id: country_id
            },
            success: function (data) {
                $('#formState').html(''); // empty the select option element
                $.each(data, function(){ // format each option returned from Pkl controller
                    $("#formState").append('<option value="'+ this.id_state +'">'+ this.nama_state +'</option>');
                });
            }
        });
    });
});

相关问题