Ajax下拉为国家状态&城市在Codeigniter? [英] Ajax drop down for Country State & City in Codeigniter?

查看:161
本文介绍了Ajax下拉为国家状态&城市在Codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的codeigniter框架工作中,使用ajax的帮助,使国家/地区和城市下降。



数据库的结构如下。



国家



  country_id,country_name 



  country_id,state_id,state_name 

city p>

  country_id,state_id,city_id,city_name 

用户控制器

  function country(){
$ data ['header'] ='交易管理';
$ data ['page'] ='admin / page / user-view';
$ data ['Country'] = $ this-> deal-> getCountry();
$ this-> load-> view($ this-> _admin_container,$ data);
}

function get_cities($ Country){
$ this-> load-> model('city_model');
header('Content-Type:application / x-json; charset = utf-8');
echo(json_encode($ this-> cities_model-> get_cities($ dealCountry)));
}

用户视图视图 b
$ b

 <?php $ cities ['#'] ='请选择'; > 

< label for =country>国家/地区:< / label><?php echo form_dropdown('country_id',$ Country,'#','id =country ); >< br />

< label for =city>城市:< / label><?php echo form_dropdown('city_id',$ cities,'#','id =cities ); >< br />

交易模块country_modle


$ b b

  function getCountry(){
$ this-> db-> select(*);
$ query = $ this-> db-> get(deal_country);
$ countries = array();
if($ query-> result()){
foreach($ query-> result()as $ country){
$ countries [$ country-> country_id] = $ country-> country_name;
}
return $ countries;
} else {
return FALSE;
}
}

city_model模块 p>

  function get_cities($ dealCountry = null){
echo $ dealCountry; die;
$ this-> db-> select('city_id,city_name');

if($ dealCountry!= NULL){
$ this-> db-> where('country_id',$ dealCountry);
}

$ query = $ this-> db-> get('deal_city');

$ cities = array();

if($ query-> result()){
foreach($ query-> result()as $ city){
$ cities [$ city-> ; id] = $ city-> city_name;
}
return $ cities;
} else {
return FALSE;
}
}

我在头文件中包含了ajax脚本。

 < script type =text / javascript> //<![CDATA [
$(document).ready(function(){
$('#country')。change(function(){// id下拉列表中的任何选择更改触发此代码
$ (#cities> option)。remove(); //首先清除选择项目
var country_id = $('#country')。val
$ .ajax({
type:POST,
url:home / get_cities /+ country_id,//这里我们调用我们的用户控制器和get_cities方法与country_id

success:function(cities)//我们调用响应json数组'cities'
{
$ .each(城市,函数)//这里我们为每个城市做一个foeach循环,id为键值,城市为值
{
var opt = $('< option />') //这里我们为每个城市创建一个新的选择选项
opt.val(id);
opt.text(city);
$('#cities')。append(opt); //这里我们将这些新的选择选项添加到id为'cities'的下拉菜单中
});
}

});

});
});
//]]>
< / script>

使代码无效后。





感谢

解决方案

  function get_cities($ Country){
$ this-> load-> model('city_model');
header('Content-Type:application / x-json; charset = utf-8');
echo(json_encode($ this-> cities_model-> get_cities($ dealCountry)));
}



在此函数中,您输入的变量名称不正确 $ dealCountry


I am making country state and city drop down with the help of ajax in our codeigniter frame work .

The structure of database given bellow.

Country

country_id,country_name

State

country_id,state_id,state_name

city

country_id,state_id,city_id,city_name

user controler

function country(){
 $data['header']='Deal Management';
 $data['page'] = 'admin/page/user-view';
 $data['Country'] = $this->deal->getCountry(); 
 $this->load->view($this->_admin_container,$data);  
}

function get_cities($Country){
  $this->load->model('city_model');
  header('Content-Type: application/x-json; charset=utf-8');
  echo(json_encode($this->cities_model->get_cities($dealCountry)));
}

user-view View

<?php $cities['#'] = 'Please Select'; ?>

<label for="country">Country: </label><?php echo form_dropdown('country_id', $Country, '#', 'id="country"'); ?><br />

<label for="city">City: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br />

deal module country_modle

 function getCountry(){
    $this->db->select("*");
    $query=$this->db->get("deal_country");
    $countries = array();
    if($query->result()){
          foreach ($query->result() as $country) {
              $countries[$country->country_id] = $country->country_name;
          }
        return $countries;
        }else{
        return FALSE;
        }
    }

city_model Module

function get_cities($dealCountry = null){
        echo $dealCountry;die;
      $this->db->select('city_id, city_name');

      if($dealCountry != NULL){
          $this->db->where('country_id', $dealCountry);
      }

      $query = $this->db->get('deal_city');

      $cities = array();

      if($query->result()){
          foreach ($query->result() as $city) {
              $cities[$city->id] = $city->city_name;
          }
      return $cities;
      }else{
          return FALSE;
      }
}

I include ajax script in header file.

<script type="text/javascript">// <![CDATA[
    $(document).ready(function(){
        $('#country').change(function(){ //any select change on the dropdown with id country trigger this code
        $("#cities > option").remove(); //first of all clear select items
            var country_id = $('#country').val();  // here we are taking country id of the selected one.
            $.ajax({
                type: "POST",
                url: "home/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id

                success: function(cities) //we're calling the response json array 'cities'
                {
                    $.each(cities,function(city_id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
                    {
                        var opt = $('<option />'); // here we're creating a new select option with for each city
                        opt.val(id);
                        opt.text(city);
                        $('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
                    });
                }

            });

        });
    });
    // ]]>
</script>

After making code its not working .

Any help is appreciated!

Thanks

解决方案

function get_cities($Country){
  $this->load->model('city_model');
  header('Content-Type: application/x-json; charset=utf-8');
  echo(json_encode($this->cities_model->get_cities($dealCountry)));
}

In this function, you put incorrect variable name $dealCountry.

这篇关于Ajax下拉为国家状态&amp;城市在Codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆