title: Dynamic Dependent Dropdown List Country and State on Code Igniter
body: <p>I am working on project that uses a dynamic or dependent dropdown list on CodeIgniter. This is my noob logic, the country value from Views passed to Models, then the Models get state data with same country value from Views, then Controllers get state data from that Models and pass it again to Views. And the problem is the state dropdown list show nothing.
  Sorry, iam new in coding. </p>
  
  <pre><code>//This is the Controllers (Pkl.php)
  $data['all_country'] = $this-&gt;Server_Model-&gt;get_country_model();
  $data['all_state'] = $this-&gt;Server_Model-&gt;get_state_model();
  $this-&gt;load-&gt;view('contents/page_dashboard', $data);
  
  //This is the Models (Server_Model.php)
  function get_country_model(){
              $db_jarlap = $this-&gt;load-&gt;database('gis_bali', TRUE);
              $db_jarlap-&gt;select("*");
              $db_jarlap-&gt;from("country");
              $que = $db_jarlap-&gt;get();
              return $que-&gt;result();
          }
  function get_state_model(){
              $kakakoko = filter_input(INPUT_POST, 'country_id');
              $db_jarlap = $this-&gt;load-&gt;database('gis_bali', TRUE);
              $db_jarlap-&gt;select("*");
              $db_jarlap-&gt;from("state");
              $db_jarlap-&gt;where(" id_country = $kakakoko ");
              $que = $db_jarlap-&gt;get();
              return $que-&gt;result();
          }
  
  //This is the Views Content (page_dashboard.php)
  &lt;script src="&lt;?php echo base_url() ?&gt;resources/js/jquery-3.3.1.min.js"&lt;/script&gt;
  &lt;script&gt;
              $(document).ready(function(){
              $('#formCountry').change(function(){
                    var country_id = $(this).val();
  
                    $.ajax({
                              url: "&lt;?php echo base_url() ?            &gt;application/models/Server_Model.php",
                          method: "POST", 
                          data: {country_id:country_id}, 
                          success: function(data) { 
                          $('#formState').html(data);
                                              }
                                      });
                                  });
                              });
  &lt;/script&gt;
  
  &lt;select name="formCountry" id="formCountry"&gt;
  &lt;?php foreach($all_country as $semua_country): ?&gt;
  &lt;option value="&lt;?php echo $semua_country-&gt;id_country; ?&gt;"&gt;&lt;?php echo     $semua_country-&gt;nama_country; ?&gt;&lt;/option&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/select&gt;
  
  &lt;select name="formState" id="formState" &gt;
  &lt;?php foreach($all_state as $semua_state): ?&gt;
  &lt;option value="&lt;?php echo $semua_state-&gt;id_state; ?&gt;"&gt;&lt;?php echo $semua_state-&gt;nama_state; ?&gt;&lt;/option&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Thanks for your help.</p>