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.
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
$this->Customer_model->get_country();
return the state and city which user had posted while registering? – NikCustomer_model->get_country();
for fetching country right? – Nik