0
votes

I am fetching data from the database in the edit page.

I am using CodeIgniter, I have two view pages, register, and edit_register.

I don't have any issue on register 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.

I submitted the form and in the database, I am getting the value like

 Country_name | state_name | city
 101          |  22        | 2707
--------------------------------------
    101-India
    22-Maharashtra
    2707-Mumbai

Let's talk about edit_register page.

Now I am on the edit_register 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.

So my issue is how to display the state and city name from the database?

Please check this when I click on edit button then I am getting only the country name but state name not displaying.

enter image description here

If I choose other country and again choose India then I am getting all the state name in the drop-down.

Controller

It's displaying my edit page with country name in the drop-down

public function search_with_number(){
  $c_mobileno=$this->input->post('c_mobileno_search');
  $got_customer_info['search_customer_info']=$this->Customer_model->get_customer_info($c_mobileno);
  $got_customer_info['get_country']=$this->Customer_model->get_country();// all country name
  $this->load->view('customer/search_order',$got_customer_info);
}

Country drop-down It's working.

<select  class="form_control country_change" name="c_s_country" data-target="target_state_dropdown2">
    <option value="" disabled selected>Select Country</option>
    <?php foreach ($get_country as $row) {?>
    <option <?php if($row->id == $post->c_s_country ){ echo 'selected="selected"'; } ?> value="<?php echo $row->id; ?>"><?php echo $row->country_name;?></option>
    <?php }?>
</select>

State drop-down It's not working

<select  class="form_control state_get" name="c_s_state" id="target_state_dropdown2" data-target="city_dropdown2">
    <option value='' disabled selected>Select state</option>
    <!--What code I have to user here-->
</select>

City It's not working

<select  class="form_control city_get" name="c_s_city" id="city_dropdown2">
    <option value="" disabled selected>Select city </option>
    <!--What code I have to user here-->
</select>

Ajax which I am using in register page

    /*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="<option value='' disabled selected>Select state</option>";
        $('#'+select_list).html(placeholder);
            $.each(data, function(i, data) {
          $('#'+select_list).append("<option value='" + data.id + "'>" + data.state_name + "</option>");
            });
      }
     });
  });
   /*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="<option value='' disabled selected>Select city</option><option value='Other'>Other</option>";
        $('#'+select_list).html(placeholder);
        $.each(data, function(i, data) {
        $('#'+select_list).append("<option value='" + data.id + "'>" + data.cities_name + "</option>");
            });
      }

     });
  });

State model

   public function get_country()
{
  $get_country = array('is_country_active' => 1);
    $this->db->where($get_country);
     $query = $this->db->get('countries');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}

public function get_state($country_id){
  $get_state = array('is_state_active' => 1,'country_id'=>$country_id);
    $this->db->where($get_state);
     $query = $this->db->get('states');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}
public function get_city($state_id){
  $get_city = array('is_city_active' => 1,'state_id'=>$state_id);
    $this->db->where($get_city);
     $query = $this->db->get('cities');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}

I haven't added the city JS.

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

1
Does your $this->Customer_model->get_country(); return the state and city which user had posted while registering?Nik
@Nik, Thanks for the reply, Yes, While registering I am getting all the country name. That's return on country name onlyuser9437856
OK. So are you missing out to select the User's preferred city and state? Where are you fetching the registered user's city and state? Like you have Customer_model->get_country(); for fetching country right?Nik
Do you mean the states are not displayed on initial page load, or not displayed when you change country?Don't Panic
I don't understand why you can't use the same code you use for the country to get the state as well(with different selection of course). Just fetch the state from the database. If i understood your code correctly you have already stored the state name in the database.nikos fotiadis

1 Answers

0
votes

In edit view, you need to do the same as you did for country. First of all, you need to fetch all your state and city from db.
Also you need to store state_id and city_id in db if you are not storing it during creation.

Now you need to pass those data to your edit view and do the same as you did for the country like this:-

<select name = "state">
<option value="">Choose State</option>  
    <?php foreach($state as $row){ ?> 
          <option value="<?php echo $row->state_id; ?>"  <?php if($row->state_id == $selected->state_id){  echo "SELECTED";}  ?>> <?php echo $row->state_id; ?></option>  
    <?php } ?>    
</select>

Do the same thing for city also. Please also remember you need to execute ajax at edit page also if the user changes the city.

This should be the code on the controller:-

public function search_with_number(){
  $c_mobileno=$this->input->post('c_mobileno_search');
  $got_customer_info['search_customer_info']=$this->Customer_model- 
   >get_customer_info($c_mobileno); //get here your saved state id of this user and match it in the view level
  $got_customer_info['get_country']=$this->Customer_model->get_country();// all country name;
  $got_customer_info['get_state']=$this->State_model->get_state();// all state name;
  $this->load->view('customer/search_order',$got_customer_info);
}