我正在codeigniter中创建一个应用程序,用于我们希望国家/地区的形式下拉国家/地区的更改选择 控制器就像
public function user()
{
$data = array();
$this->load->model('enquiry');
$this->load->helper('url');
$data['result'] = $this->enquiry->get_reqtutor();
$data['countries'] = $this -> country_model -> get_countries();
$this->load->view('Table\DemoHistory',$data);
}
function get_states($country) {
$this->load->helper('url');
$this->load->model('state_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->state_model->get_states($country)));
}
和模型就好 state_model `
<?php
class State_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
function get_states($country = null){
$this->db->select('state_id, state_name');
if($country != NULL){
$this->db->where('country_id', $country);
}
$query = $this->db->get('state_master');
$states = array();
if($query->result()){
foreach ($query->result() as $state) {
$cities[$state->state_id] = $state->state_name;
}
return $states;
} else {
return FALSE;
}
}
}
?>
country_model
<?php
class Country_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
function get_countries() {
$this -> db -> select('country_id, country_name');
$query = $this -> db -> get('country_master');
$countries = array();
if ($query -> result()) {
foreach ($query->result() as $country) {
$countries[$country -> country_id] = $country -> country_name;
}
return $countries;
} 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: /mtb/Schedule/get_states/"+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(state_id,state_name) //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(state_id);
opt.text(state_name);
$('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
});
}
});
});
});
// ]]>
</script>
并且查看
<?php $countries['#'] = 'Please Select'; ?>
<label for="country">Country: </label><?php echo form_dropdown('country_id', $countries, '#', 'id="country"'); ?>
<?php $cities['#'] = 'Please Select'; ?>
<label for="city">State: </label><?php echo form_dropdown('state_id', $cities, '#', 'id="cities"'); ?>
我正在获取国家/地区名称,而我的状态值不会在下拉列表中显示,请帮助........
答案 0 :(得分:0)
您需要考虑的代码:
你在这里有一个错误网址:/mtb/Schedule/get_states/"+country_id
,在开始时缺少双引号。始终使用调试工具,如firebug,它会显示javascript错误。
另一件事,代码
if($country != NULL){
应该是
if(!is_null($country)){
另一件事,你应该使用application/x-json
而不是application/json
json标题。
另一件事,代码
echo(json_encode($this->state_model->get_states($country)));
应该是
echo(json_encode($this->state_model->get_states($country)));
exit;
否则可能会将意外结果附加到最终的json。 Example:
CodeIgniter分析。
另一件事,代码
$countries = array();
if ($query -> result()) {
foreach ($query->result() as $country) {
$countries[$country -> country_id] = $country -> country_name;
}
return $countries;
} else {
return FALSE;
}
应该是
$countries = array();
foreach ($query->result() as $country) {
$countries[$country -> country_id] = $country -> country_name;
}
return $countries;
因为没有国家/地区且返回FALSE
会在form_dropdown
函数中出现错误,请查看其中。 get_states
函数也是如此。
这是我在代码的初步看法中看到的内容。