title: How to display the state and city name from the database on edit page?
body: <p>I am fetching data from the database in the edit page.</p>
  
  <p>I am using CodeIgniter, I have two view pages, <code>register</code>, and <code>edit_register</code>.</p>
  
  <p>I don't have any issue on <code>register</code> page but still, I am sharing the process to understand my issue. On this page country, state, city dependancy is also working. I mean I choose "India" then It will display all state name in state dropdown. Now I choose "Maharashtra" then I choose "Mumbai". Working perfectly.</p>
  
  <p>I submitted the form and in the database, I am getting the value like   </p>
  
  <pre><code> Country_name | state_name | city
   101          |  22        | 2707
  --------------------------------------
      101-India
      22-Maharashtra
      2707-Mumbai
  </code></pre>
  
  <p>Let's talk about <code>edit_register</code> page.</p>
  
  <p>Now I am on the <code>edit_register</code> page, I am fetching the country name in the dropdown. I am getting the correct output "India" but I am not getting the state and city name.</p>
  
  <p>So my issue is how to display the state and city name from the database?</p>
  
  <p>Please check this when I click on edit button then I am getting only the country name but state name not displaying.</p>
  
  <p><a href="https://i.stack.imgur.com/Egbgq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Egbgq.png" alt="enter image description here"></a></p>
  
  <p>If I choose other country and again choose India then I am getting all the state name in the drop-down.</p>
  
  <p><strong>Controller</strong></p>
  
  <p>It's displaying my edit page with country name in the drop-down</p>
  
  <pre><code>public function search_with_number(){
    $c_mobileno=$this-&gt;input-&gt;post('c_mobileno_search');
    $got_customer_info['search_customer_info']=$this-&gt;Customer_model-&gt;get_customer_info($c_mobileno);
    $got_customer_info['get_country']=$this-&gt;Customer_model-&gt;get_country();// all country name
    $this-&gt;load-&gt;view('customer/search_order',$got_customer_info);
  }
  </code></pre>
  
  <p><strong>Country drop-down</strong> It's working.</p>
  
  <pre><code>&lt;select  class="form_control country_change" name="c_s_country" data-target="target_state_dropdown2"&gt;
      &lt;option value="" disabled selected&gt;Select Country&lt;/option&gt;
      &lt;?php foreach ($get_country as $row) {?&gt;
      &lt;option &lt;?php if($row-&gt;id == $post-&gt;c_s_country ){ echo 'selected="selected"'; } ?&gt; value="&lt;?php echo $row-&gt;id; ?&gt;"&gt;&lt;?php echo $row-&gt;country_name;?&gt;&lt;/option&gt;
      &lt;?php }?&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><strong>State drop-down</strong> It's not working</p>
  
  <pre><code>&lt;select  class="form_control state_get" name="c_s_state" id="target_state_dropdown2" data-target="city_dropdown2"&gt;
      &lt;option value='' disabled selected&gt;Select state&lt;/option&gt;
      &lt;!--What code I have to user here--&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><strong>City</strong> It's not working</p>
  
  <pre><code>&lt;select  class="form_control city_get" name="c_s_city" id="city_dropdown2"&gt;
      &lt;option value="" disabled selected&gt;Select city &lt;/option&gt;
      &lt;!--What code I have to user here--&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Ajax which I am using in register page</p>
  
  <pre><code>    /*Get all the state name using country code*/
     $(".country_change").on('change',function(){
        var country_id=$(this).val();
        var select_list = $(this).data('target'); // The dropdown ID
    $.ajax({
        url:baseUrl +"/Customer_control/statename",
        method:"POST",
        data:"country_id="+country_id,
       dataType: "json",
        success:function(data){
          $('#'+select_list).empty();
          var placeholder="&lt;option value='' disabled selected&gt;Select state&lt;/option&gt;";
          $('#'+select_list).html(placeholder);
              $.each(data, function(i, data) {
            $('#'+select_list).append("&lt;option value='" + data.id + "'&gt;" + data.state_name + "&lt;/option&gt;");
              });
        }
       });
    });
     /*Get all the city name using state code*/
     $(".state_get").on('change',function(){
        var state_id=$(this).val();
        var select_list = $(this).data('target'); // The dropdown ID
    $.ajax({
        url:baseUrl +"/Customer_control/cityname",
        method:"POST",
        data:"state_id="+state_id,
        dataType: "json",
        success:function(data){
          $('#'+select_list).empty();
          var placeholder="&lt;option value='' disabled selected&gt;Select city&lt;/option&gt;&lt;option value='Other'&gt;Other&lt;/option&gt;";
          $('#'+select_list).html(placeholder);
          $.each(data, function(i, data) {
          $('#'+select_list).append("&lt;option value='" + data.id + "'&gt;" + data.cities_name + "&lt;/option&gt;");
              });
        }
  
       });
    });
  </code></pre>
  
  <p><strong>State model</strong></p>
  
  <pre><code>   public function get_country()
  {
    $get_country = array('is_country_active' =&gt; 1);
      $this-&gt;db-&gt;where($get_country);
       $query = $this-&gt;db-&gt;get('countries');
        $result = $query-&gt;result();
        if($result)
        {
          return $result;
        }
        else 
        {
          return 0;
        } 
  }
  
  public function get_state($country_id){
    $get_state = array('is_state_active' =&gt; 1,'country_id'=&gt;$country_id);
      $this-&gt;db-&gt;where($get_state);
       $query = $this-&gt;db-&gt;get('states');
        $result = $query-&gt;result();
        if($result)
        {
          return $result;
        }
        else 
        {
          return 0;
        } 
  }
  public function get_city($state_id){
    $get_city = array('is_city_active' =&gt; 1,'state_id'=&gt;$state_id);
      $this-&gt;db-&gt;where($get_city);
       $query = $this-&gt;db-&gt;get('cities');
        $result = $query-&gt;result();
        if($result)
        {
          return $result;
        }
        else 
        {
          return 0;
        } 
  }
  </code></pre>
  
  <p>I haven't added the city JS.</p>
  
  <p>Now Please check my Ajax code, My state name is changing when I select a country name.  I have to display the state and city  name onload from the database</p>