title: How to get data from API with ajax on select (dropdown option value) in Codeigniter?
body: <p>I have a case in my code, the result does not appear or work, the city form select is still blank. Here is the HTML:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div class="form-group"&gt;
    &lt;label&gt;Province&lt;/label&gt;
    &lt;select class="form-control" name="province" id="province"&gt;
  
      &lt;?php foreach($state_data-&gt;data as $sd){ ?&gt;
      &lt;option id="&lt;?php echo $sd-&gt;code; ?&gt;" value="&lt;?php echo $sd-&gt;code; ?&gt;" &lt;?php echo (isset($this-&gt;session-&gt;userdata))?(($this-&gt;session-&gt;userdata('session_state_code') == $sd-&gt;code)?"selected":""):"";?&gt;&gt;
        &lt;?php echo $sd-&gt;name ;?&gt;
      &lt;/option&gt;
      &lt;?php } ?&gt;
    &lt;/select&gt;
  &lt;/div&gt;
  &lt;div class="form-group" id="city_section"&gt;
    &lt;label&gt;City&lt;/label&gt;
    &lt;select class="form-control" name="city" id="city"&gt;
  
      &lt;?php foreach($city_data-&gt;data as $cd){ ?&gt;
      &lt;option value="&lt;?php echo $cd-&gt;code; ?&gt;" &lt;?php echo (isset($this-&gt;session-&gt;userdata))?(($this-&gt;session-&gt;userdata('session_city_code') == $cd-&gt;code)?"selected":""):"";?&gt;&gt;
        &lt;?php echo $cd-&gt;name ;?&gt;
      &lt;/option&gt;
      &lt;?php } ?&gt;
    &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This the javascript to get data from the controller:</p>
  
  <pre class="lang-js prettyprint-override"><code>$('#province').on('change', function() {
        var id = $(this).val();;
        $.ajax({
          type: "POST",
          dataType: 'json',
          url: "&lt;?php echo base_url() ?&gt;edit_profile/get_city",
          data: {
            id: $(this).val()
          },
          success: function(data) {
            $('select#city').html('');
            for (var i = 0; i &lt; data.length; i++) {
              $("&lt;option /&gt;").val(data[i].code)
                .text(data[i].name)
                .appendTo($('select#city'));
            }
          }
        });
      }
  </code></pre>
  
  <p>and this the controller to get data from API:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function get_city(){
    $m_url = URL_API;
    $function = "get_location";
    $i = $_REQUEST['id'];
    $url = $m_url . "/" . $function . "?" . "t=city" . "&amp;" . "i=".$i;
    $json_data = file_get_contents($url);
    $data = json_decode($json_data);  
  }
  </code></pre>