I am selecting COUNTRY from First Dropdown Box and then based on country STATE Values are populated in second Dropdown Box and Then based on State CITY values are populated in Select option element.
My code :
**Controller :**
function index()
{
$this->load->model('User_model');
$country = $this->User_model->fetch_country();
$data = array();
$data['country'] = $country;
$this->load->view('create',$data);
}
function fetch_state()
{
$this->load->model('User_model');
if($this->input->post('country_id'))
{
$this->User_model->fetch_state($this->input->post('country_id'));
}
}
function fetch_city()
{
$this->load->model('User_model');
if($this->input->post('state_id'))
{
$this->User_model->fetch_city($this->input->post('state_id'));
}
}
**Module**:
function fetch_country()
{
$this->db->order_by('country_name','ASC');
$query = $this->db->get('country');
return $query->result();
}
function fetch_state($country_id)
{
$this->db->where('country_code',$country_id);
$this->db->order_by('state_name','ASC');
$query = $this->db->get('state');
$output = $query->result();
$output = '<option value="" selected="disabled">Select state</option>';
foreach ($query->result() as $state)
{
$output .= '<option value= "'.$state->state_code. '" selected="SELECTED">' . $state->state_name .'</option>';
}
echo $output;
return $output;
}
function fetch_city($state_id)
{
$this->db->where('state_code',$state_id);
$this->db->order_by('district_name','ASC');
$query = $this->db->get('city');
$output = $query->result();
$output = '<option value="" selected="disabled">Select state</option>';
foreach ($query->result() as $city)
{
$output .= '<option value= "'.$city->district_code. '" selected="selected">' . $city->district_name .'</option>';
}
echo $output;
return $output;
}
**View :**
<div class="form-group">
<label for="role">Country</label>
<select class="form-control custom-select" value="<?php echo set_value('country');?>" name="country" id="country">
<option value="" selected="disabled">Select country</option>
<?php
if(!empty($country))
{
foreach ($country as $r2)
{
?>
<option value="<?php echo $r2->country_id ?>" <?php echo set_select('country',$r2->country_id); ?><?php if($r2->country_id == set_value('country_name')) {echo "selected=selected";} ?>><?php echo $r2->country_name; ?></option>
<?php
}
}
?>
</select>
<?php echo form_error('country');?>
</div>
<div class="form-group">
<label for="role">State</label>
<select class="form-control custom-select" value="<?php echo set_value('state');?>" name="state" id="state">
<option value="" selected="disabled">Select State</option>
</select>
<?php echo form_error('state');?>
</div>
<div class="form-group">
<label for="role">City</label>
<select class="form-control custom-select" value="<?php echo set_value('city');?>" name="city" id="city">
<option value="" selected="disabled">Select City</option>
</select>
<?php echo form_error('City');?>
</div>
<div class="form-group">
<button class="btn btn-primary" id="submit_data" name="submit_data">Create</button>
<a href="<?php echo base_url().'index.php/user/index'; ?>" class="btn-secondary btn">Cancel</a>
</div>
<script>
$(document).ready(function(){
$('#country').change(function(){
var country_id = $('#country').val();
if(country_id != '')
{
$.ajax({
url:"<?php echo base_url();?>index.php/user/fetch_state",
method:"POST",
data:{country_id:country_id},
success:function(data)
{
$('#state').html(data);
$('#city').html('<option value="">Select City</option>');
}
});
}
else
{
$('#state').html('<option value="">Select State</option>');
$('#city').html('<option value="">Select City</option>');
}
});
$('#state').change(function(){
var state_id = $('#state').val();
if(state_id != '')
{
$.ajax({
url:"<?php echo base_url(); ?>index.php/user/fetch_city",
method:"POST",
data:{state_id:state_id},
success:function(data)
{
$('#city').html(data);
}
});
}
else
{
$('#city').html('<option value="">Select City</option>');
}
});
});
</script>
После загрузки и выбора всех данных в динамической форме c в соответствии с пользователем wi sh. Теперь пытаемся нажать кнопку CREATE. Предположим, одно поле не заполнено. во время бросания ошибки и значения State и City Values исчезают. Как сохранить значения из того же выпадающего списка в CodeIgniter.