I have this general question. may it's too vast to answer as there could be several explanations about it. But I am beginner and can't understand it.
I have multiple interdependent dropdowns (i.e. next loads on selection of first, like for city, state, region, city).
In one of my controller function they all work fine. (When I post a new listing).
I created a second function with same methodology that I used for first (only difference is I have URI->segment in it).
Now what I have discovered if I load path for this second function without putting id, it works fine but if I add .../43
(or any id) second dropdown never loads.
Please suggest..
Controller Function where it doesn't works
public function add_new_listing() {
$id = $this->uri->segment(3);
$this->load->model('admin/model_users');
$data['list'] = $this->model_users->getCountry();
$this->load->view('view', $data);
}
public function loadData() {
$loadType=$_POST['loadType'];
$loadId=$_POST['loadId'];
$this->load->model('admin/model_users');
$result=$this->model_users->getData($loadType,$loadId);
$HTML="";
if($result->num_rows() > 0){
foreach($result->result() as $list){
$HTML.="<option value='".$list->id."'>".$list->name."</option>";
}
}
echo $HTML;
}
Model:
function getCountry(){
$this->db->select('v_country_id,v_country_name');
$this->db->from('vbc_country');
$this->db->order_by('v_country_name', 'asc');
$query=$this->db->get();
return $query;
}
function getData($loadType,$loadId){
if($loadType=="state"){
$fieldList='id,v_state_name as name';
$table='vbc_state';
$fieldName='country_id';
$orderByField='v_state_name';
}elseif($loadType == "region"){
$fieldList='id,v_state_region_name as name';
$table='vbc_state_region';
$fieldName='state_id';
$orderByField='v_state_region_name';
}else{
$fieldList='id,v_city_name as name';
$table='vbc_city';
$fieldName='state_region_id';
$orderByField='v_city_name';
}
$this->db->select($fieldList);
$this->db->from($table);
$this->db->where($fieldName, $loadId);
$this->db->order_by($orderByField, 'asc');
$query=$this->db->get();
return $query;
}
And view:
<select name="v_item_country" onchange="selectState(this.options[this.selectedIndex].value)">
<option value="0">Select Country</option>
<?php foreach($list->result() as $listElement): ?>
<option value="<?= $listElement->v_country_id?>"><?= $listElement->v_country_name?></option>
<?php endforeach; ?>
</select>
<select name="v_item_state" id="state_dropdown" onchange="selectRegion(this.options[this.selectedIndex].value)">
<option value="0">Select State</option>
</select>
<select name="v_item_region" id="region_dropdown" onchange="selectCity(this.options[this.selectedIndex].value)">
<option value="0">Select Region</option>
</select>
<select name="v_item_city" id="city_dropdown">
<option value="0">Select City</option>
</select>
JS Code:
function selectState(country_id){
if(country_id!="0"){
loadData('state',country_id);
$("#region_dropdown").html("<option value='0'>Select Region</option>");
}else{
$("#state_dropdown").html("<option value='0'>Select state</option>");
$("#region_dropdown").html("<option value='0'>Select Region</option>");
}
}
function selectRegion(state_id){
if(state_id!="0"){
loadData('region',state_id);
$("#city_dropdown").html("<option value='0'>Select city</option>");
}else{
$("#region_dropdown").html("<option value='0'>Select Region</option>");
$("#city_dropdown").html("<option value='0'>Select city</option>");
}
}
function selectCity(state_region_id){
if(state_region_id!="0"){
loadData('city',state_region_id);
}else{
$("#city_dropdown").html("<option value='0'>Select city</option>");
}
}
function loadData(loadType,loadId){
var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
$("#"+loadType+"_loader").show();
$("#"+loadType+"_loader").fadeIn(400).html('Please wait... <img src="image/loading.gif" />');
$.ajax({
type: "POST",
url: "loadData",
data: dataString,
cache: false,
success: function(result){
$("#"+loadType+"_loader").hide();
$("#"+loadType+"_dropdown").html("<option value='0'>Select "+loadType+"</option>");
$("#"+loadType+"_dropdown").append(result);
}
});
}