根据codeigniter中的组合框选择显示特定数据

时间:2016-10-04 16:15:47

标签: javascript php codeigniter combobox

我有两个组合框......我想根据第一个组合框显示第二个组合框数据。 这是我的代码

为MyModel

function findsprovince() {
    $this->db->select('*');
    $this->db->from('tbl_province');
    $query = $this->db->get();
    return ($query->num_rows() > 0) ? $query->result() : null;
}

function findscity() {
    $this->db->select('*');
    $this->db->from('tbl_city');
    $query = $this->db->get();
    return ($query->num_rows() > 0) ? $query->result() : null;
}


function findsCity_by_id($id){
    $this->db->select('*');
    $this->db->where('tbl_city.id_city',$id); 
    return $this->db->get('tbl_city')->row();
}

mycontrollers

function Add() {
    $data_user = $this->session->userdata('data_user');
    if (($this->session->userdata('data_user') == "")) {
        redirect('admin/login');
    } else {

        $province = $this->pura_m->findsprovince();
        $select_province = array();
        $select_province[""] = "- Select province -";
        if (!empty($province)) {
            foreach ($province as $prd) {
                $select_province[$prd->id_province] = $prd->name_province;
            }
        }

        $city = $this->pura_m->findscity();
        $select_city = array();
        $select_city[""] = "- Select city -";
        if (!empty($city)) {
            foreach ($city as $kc) {
                $select_city[$kc->id_city] = $kc->name_city;
            }
        }

        $data = array(
            'nama_user' => $data_user['nama_user'],
            'getprovince' => $select_province,
            'getcity' => $select_city,
            'content' => 'pura/create',
            'detail' => '',
        );
        $this->load->view("layout/main", $data);
    }
}

和这个观点

<form action="<?php echo base_url() ?>pura/create" method="post" enctype="multipart/form-data" class="form-horizontal" role="form">
    <div class="form-group ">
        <label class="col-sm-2 control-label">Nama Kota</label>
        <div class="col-sm-6">
            <?php echo form_dropdown('id_province', $getprovince,  '' ,"class='form-control' onchange='showCity(this.value)' id='id_province' required "); ?>
        </div>
    </div>
    <div id="city">
        <div class="form-group ">
            <label class="col-sm-2 control-label">Name City</label>
            <div class="col-sm-6">
                <?php echo form_dropdown('id_city', $getcity,  '' ,"class='form-control' id='id_city' required "); ?>
            </div>
        </div>
    </div>


    <div class="form-group">
        <div class="col-sm-2"></div>
        <div class=" col-sm-3">
                <button type="submit" class="btn btn-success">Simpan</button>
        </div>
    </div>

</form>
<script type="text/javascript">
        function showCity(str) 
    {

        if (str == "") {
            $('#id_city').val('');
            return;
        } else { 
          if (window.XMLHttpRequest) {
              // code for IE7+, Firefox, Chrome, Opera, Safari
               xmlhttp = new XMLHttpRequest();
          } else {
              // code for IE6, IE5
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("kota").innerHTML = 
                  xmlhttp.responseText;
              }
          }
          xmlhttp.open("GET", "<?= base_url(
            'pura/getcity') ?>/"+str,true);
          xmlhttp.send();
        }
    }
</script>

这是我在mycontroller中的功能

function getAnggota($id){

    $city1= $this->pura_m->findsCity_by_id($id);
    $select_city1 = array();
    $select_city1[""] = "- Select city -";
    if (!empty($city1)) {
        foreach ($city1 as $kd) {
            $select_city1[$kd->id_city] = $kd->name_city;
        }
    }

    if($select_city1){
        echo '<div class="form-group ">
                <label class="col-sm-2 control-label">Name City</label>
                <div class="col-sm-6">
                    <?php echo form_dropdown("id_city" ,"'. $select_city1 . '",  "" ,"class="form-control" id="id_city" required "); ?>
                </div>
            </div';
    }
}

我尝试在combobox 1选项上添加一个on click功能,但是当我点击它们时它没有工作......那么有一些方法可以这样做吗?

0 个答案:

没有答案