首页 > 解决方案 > 无法填充第二个下拉列表

问题描述

为什么每次我从“国家/地区”中选择时,它都不会填充该特定国家/地区的州列表。我有两张桌子,一张是国家的,另一张是州的。

private $_table = "tbl_country";
        ## MY MODEL ##
        function get_by_id($id)
          {
            $query = $this->db->get_where($this->_table, "ID = $id");

            $data = $query->row();
            $query->free_result();
            return $data;
          }

         function fetchCountry()
          {
            $this->db->select('Country');
            $this->db->from('tbl_country');
            $this->db->order_by('Country', "asc");
            $get = $this->db->get();
            return $get->result_Array();
          }

          function fetchState($data)
          {
            $this->db->select('State');
            $this->db->from('tbl_state');
            $this->db->where($data);
            $this->db->order_by('State', "asc");
            $get = $this->db->get();
            return $get->result_Array();
          }

1.这是我的看法php

     <div class="form-group">
       <label>Country</label>
        <?= form_dropdown('Country', [
            ''  => '--Select Country--',
            'United States' => 'United States'
            ], set_value('Country', @$rs->Country? $rs->Country: ''), 'class="form-control"id="Country"') ?>
        <?= form_error('Country') ?>
        </div>
       <div class="form-group">
        <label>State</label>
        <select name="State" id="State" class="form-control">
        <option value="" selected disabled>--Select State--</option>
        </select>
  1. 控制器

    函数 showCountry() { $result = $this->country_m->fetchCountry(); 回声 json_encode($result);
    }

    function showState()
    {
        $data = $this->input->post();
        $result = $this->country_m->fetchState($data);
        echo json_encode($result);
    }
    

标签: phpmysqlcodeigniter

解决方案


您不会以这种方式获得它。您必须使用 Ajax 功能,因为它们是相关的下拉菜单。


推荐阅读