title: getting values of second select from db based of first select box selection in codeigniter
body: <p>I Need help on how can i get values of second select box based on first select box</p>
  
  <p>This is view:</p>
  
  <pre><code>$(document).ready(function() {
  
  $('#state').change(function() {
  // Get an instance of the select, and it's value.
      var state = $(this),
      stateID = state.val();
  // Add if statement to check if the new state id
  // is different to the last to prevent loading the same
  // data again.
  
  // Do the Ajax request.
  $.ajax({
      url : 'http://localhost/ci_ajax/select/get_cities/'+stateID, // Where to.
      dataType : 'json', // Return type.
      success : function(data) { // Success :)
          // Ensure we have data first.
          if(data &amp;&amp; data.Cities) {
              // Remove all existing options first.
              state.find('option').remove();
              // Loop through each city in the returned array.
              for(var i = 0; i &lt;= data.Cities.length; i++) {
                  // Add the city option.
                  state.append($('option').attr({
                      value : data.Cities[i].value
                  }).text(data.Cities[i].city));
              }
          }
      },
      error : function() {
          // Do something when an error happens?
      }
  });
  </code></pre>
  
  <p>});
  });</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  &lt;select name="state" id="state"&gt;
      &lt;option&gt;Select&lt;/option&gt;
      &lt;?php foreach($states as $row):?&gt;
          &lt;option value="&lt;?php echo $row-&gt;id?&gt;"&gt;&lt;?php echo $row-&gt;states;?&gt;&lt;/option&gt;
      &lt;?php endforeach;?&gt;
  &lt;/select&gt;
  &lt;select id="cities" name="cities"&gt;
      &lt;option&gt;Select&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is controller:
  
  
  <p>class Select extends CI_Controller{</p>
  
  <pre><code>function index(){
  
      $data['states']=$this-&gt;load_state();
      $this-&gt;load-&gt;view('form',$data);
  }
  
  function load_state(){
  
      $this-&gt;load-&gt;model('data_model');
  
      $data['states']=$this-&gt;data_model-&gt;getall_states();
  
      return $data['states'];
  }
  
  function get_cities() {
       // Load your model.
      $this-&gt;load-&gt;model('data_model');
      // Get the data.
      $cities = $this-&gt;data_model-&gt;get_cities();
      // Specify that we're returning JSON.
      header('content-type: application/json');
      // Return a JSON string with the cities in.
      return json_encode(array('Cities' =&gt; $cities));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is model:</p>
  
  <p>
  
  <p>class Data_model extends CI_Model{</p>
  
  <pre><code>function getall_states(){
  
      $query=$this-&gt;db-&gt;get('states');
      if($query-&gt;num_rows()&gt;0){
          foreach($query-&gt;result() as $row){
              $data[]=$row;
          }
          return $data;
      }
  
  }
  
  function get_cities(){
  
      $this-&gt;db-&gt;select('id,cities');
      $this-&gt;db-&gt;from('cities');
      $this-&gt;db-&gt;where('s_id',$this-&gt;uri-&gt;segment(3));
      $query=$this-&gt;db-&gt;get();
  
      if($query-&gt;num_rows()&gt;0){
          foreach($query-&gt;result() as $row){
              $data[]=$row;
          }
          return $data;
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help on this hopefully provide the correct code.</p>