Php Ajax下拉列表,用于国家和地区;代码点火器中的城市?
在我们的codeigniter框架中,我借助ajax使country state和city下拉列表 下面给出了数据库的结构 国家Php Ajax下拉列表,用于国家和地区;代码点火器中的城市?,php,codeigniter,drop-down-menu,Php,Codeigniter,Drop Down Menu,在我们的codeigniter框架中,我借助ajax使country state和city下拉列表 下面给出了数据库的结构 国家 country_id,country_name country_id,state_id,city_id,city_name 状态 country_id,state_id,state_name function getCountry(){ $this->db->select("*"); $query=$this->db->
country_id,country_name
country_id,state_id,city_id,city_name
状态
country_id,state_id,state_name
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;
}
}
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;
}
}
城市
country_id,country_name
country_id,state_id,city_id,city_name
用户控制器
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)));
}
用户视图
<?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 />
城市模型模块
country_id,state_id,state_name
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;
}
}
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(){ //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>
/创建一个javascript变量
var base_url = "<?=base_url()?>";
使用firefox和firebug调试ajax代码正如我在评论中所说,您可以使用或(网络选项卡)来确定您的问题:
控制器:
<?php
class ctrl extends ci_controller{
function index(){ //I just used this one to test out if its outputing anything
$names = ['apple','ball','cat','dog'];
echo json_encode($names);
}
function load_view(){
$this->load->view('ctrl_view');
}
}
?>
视图:
$.post('ctrl/',函数(数据){//您可以在此处调用控制器
var parsed_data=JSON.parse(数据);
//使用文档片段将新元素附加到dom中,而不是在dom中现有元素的循环的每次迭代中都附加新元素,这是一种很好的做法
var fragment=document.createDocumentFragment();
for(解析的_数据中的变量x){
var val=已解析的_数据[x];
var选项=$(“”).val(val).text(val);
fragment.appendChild(选项[0]);
}
$('#names').append(fragment);//将整个片段附加到循环之外
});
我真的不知道你的实际问题是什么,所以请帮个忙,编辑你的问题并粘贴你在firebug上看到的错误
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到底是什么不起作用?ajax调用没有返回任何内容吗?您是否使用firebug或chrome开发工具来识别您的问题?ajax不会返回任何东西!帮个忙,把你在firebug上看到的错误贴出来。您的问题很可能是路径中的问题。您可能希望首先将基本url分配给config.php文件中的空字符串。