title: How to reduce the loading time of a function in ajax?
body: <p>This is my <a href="http://attila-naghi.com/register" rel="nofollow">site</a>. The idea is when you select a country from the dd, by on change events its calls a function using AJAX, execute a query and return a json. Im using Codeigniter framework. So my question how to improve this code in order to reduce the loading time of the cities, by selectin a country ? yes i know im a little stubborn and i dont want to use counties :)</p>
  
  <p>here is html code in the view: </p>
  
  <pre><code>&lt;div class="select_zones"&gt;
  Country:
  &lt;select name="country" id = 'country' style="width:220px;"&gt;
  &lt;option value = "-1" selected="selected"&gt;Select a country&lt;/option&gt;
  &lt;?php 
      foreach ($countries as $key =&gt; $value) {
              echo '&lt;option value = "'.$value-&gt;id.'-'.$value-&gt;country_type.'"&gt;'.trim($value-&gt;name).'&lt;/option&gt;';
      }
  ?&gt;
  &lt;/select&gt;
  &lt;/div&gt;
  &lt;div class="select_zones"&gt;
      &lt;div class="city" style ="display:none;"&gt;
          City:
          &lt;select name="city" id ="city" style="width:220px;"&gt;
          &lt;/select&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>this is the js code:</p>
  
  <pre><code>$("#country").select2(); 
      $("#city").select2();
      $("#country").change(function(){
          $("#city option").remove();
          value = $(this).val();
          value = value.split("-");
          $.post( "register/get_cities_from_dd", { country_type: value[1]})
              .done(function( data ) {
                  obj = JSON.parse(data);
                  for(var i in obj) {
                      $('&lt;option&gt;', {
                          text     : obj[i].name,
                          value    : obj[i].id
                      }).prependTo('#city');
                  }
          });
          $(".city").show();
      });
  </code></pre>
  
  <p>this is the function from the controller: </p>
  
  <pre><code>   public function get_cities_from_dd(){
  
          $country_type = $_POST['country_type'];
          $cities = $this-&gt;register_model-&gt;get_cities($country_type);
          echo json_encode($cities);
  
      }
  </code></pre>
  
  <p>and finally the model:</p>
  
  <pre><code> public function get_cities($country_type){
  
          return $this-&gt;db-&gt;query("SELECT id,name FROM cities WHERE city_type = '".$country_type."' ORDER BY `name` DESC ")-&gt;result();
  
      }
  </code></pre>