dongyong3223 2016-01-24 18:17
浏览 39
已采纳

相互依赖的下拉列表不适用于uri段,但没有它们在Codeigniter中

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);  
        }
    });
}
  • 写回答

1条回答 默认 最新

  • douhuang2673 2016-01-24 18:41
    关注

    I am pretty sure that public function add_new_listing() needs a param for it to be loaded. Try this: public function add_new_listing($param = null)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿