代码点火器 - 表格下拉列表从数据库中选择正确的值(州和城市使用javascript)

时间:2013-04-12 01:44:36

标签: php javascript html codeigniter

我有类似这样的问题:Code Igniter - form_dropdown selecting correct value from the database,但在这种情况下,我有2个下拉菜单,状态&城市(使用JavaScript)。 City的下拉选项根据用户在State下拉列表中选择的内容重新填充。

例如,当用户选择州(例如:纽约)时,城市的下拉选项仅变为纽约的城市(例如:奥尔巴尼,阿姆斯特丹等)。

用户选择一个值后,点击保存,保存到数据库。

问题是,如何让下拉菜单自动选择用户在初始阶段选择的那个?如果它只有1个下拉选项,我可以做到。但在这种情况下,City的下拉列表会根据用户在State下拉选项中选择的内容重新填充。

控制器:

//this one i managed to get it automatically choose the one that's been selected by the user in the initial stage    
$username=$this->session->userdata('username'); 
$data['orgtype'] = $this->m_user->get_orgtype_dropdown($username);

//these two are the problem 
$data['state'] = $this->m_user->get_state_dropdown($username); 
$data['city'] = $this->m_user->get_city_dropdown($username);

型号:

function get_orgtype_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);
   foreach($hslquery->result_array() as $row){
        $return[$row['orgtype']] = $row['orgtype'];
        }
    return $return;
}

function get_state_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);       
   foreach($hslquery->result_array() as $row){
        $return[$row['state']] = $row['state'];
        }
    return $return;
}

function get_city_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);       
   foreach($hslquery->result_array() as $row){
        $return[$row['city']] = $row['city'];
        }
    return $return;
}

查看:

<?php
$orgtypeOption = array(
         'Academic'  => 'Academic',
         'Professional'    => 'Professional',                  
         );

    echo form_label("Organization Type : ");
    echo form_dropdown('orgtype', $orgtypeOption, $orgtype);
    echo br();
    echo form_label("State : ");            
    ?>
    <select name ="state" id="countrySelect" size="1" onChange="makeSubmenu(this.value)">
        <option></option>
        <option value="USA" <?php if ($state=="USA") echo 'selected="selected"';?>>USA</option>
        <option value="Singapore" <?php if ($state=="Singapore") echo 'selected="selected"';?>>Singapore</option>
        <option value="Jawa Timur" <?php if ($state=="Jawa Timur") echo 'selected="selected"';?>>Jawa Timur</option>
        <option value="Jawa Barat" <?php if ($state=="Jawa Barat") echo 'selected="selected"';?>>Jawa Barat</option>
    </select>
    <?php
    echo br();
    echo form_label("City : ");
    ?>
    <select name="city" id="citySelect" size="1">
        <option></option>
    </select>

JavaScript的:

var citiesByState = {
    USA: ["NY","NJ"],
    Singapore: ["taas","naas"],
    "Jawa Timur": ["Surabaya","Malang"],
    "Jawa Barat": ["Bandung","Banjar"]
};
function makeSubmenu(value) {
    if(value.length==0) document.getElementById("citySelect").innerHTML = "<option></option>";
    else {
        var citiesOptions = "";
        for(cityId in citiesByState[value]) {
            citiesOptions+="<option>"+citiesByState[value][cityId]+"</option>";
        }
        document.getElementById("citySelect").innerHTML = citiesOptions;
    }
}
function displaySelected() {
    var country = document.getElementById("countrySelect").value;
    var city = document.getElementById("citySelect").value;
    alert(country+"\n"+city);
}
function resetSelection() {
    document.getElementById("countrySelect").selectedIndex = 0;
    document.getElementById("citySelect").selectedIndex = 0;
}

预览:application preview picture

请帮助

0 个答案:

没有答案