我正在尝试使用codeigniter 2.1.4中的ajax从州获取城市。我面临的问题是取代整个html索引页面的数据。无法解决问题请帮忙。我的代码如下:
class Pages extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('home_model');
$this->load->model('sub_cat_model');
$this->load->model('state_model');
$this->load->model('city_model');
$this->load->model('location_model');
$this->load->library('email');
}
public function index(){
$data['state'] = $this->home_model->get_state();
$data['title'] = 'Rimi Classified - Home';
$this->load->view('templates/header', $data);
$this->load->view('index', $data);
$this->load->view('templates/footer', $data);
}
public function sign_up(){
$data['states'] = $this->state_model->get_states();
$data['error'] = '';
$data['title'] = 'Rimi Classified - Sign up';
$this->load->view('templates/header1', $data);
$this->load->view('sign-up', $data);
$this->load->view('templates/footer', $data);
}
public function get_cities($state){
header('Content-Type: application/x-json; charset=utf-8');
die(json_encode($this->city_model->get_cities($state)));
}
}
class City_model extends CI_Model{
public function __construct(){
$this -> load -> database();
//$this->output->enable_profiler(TRUE);
}
function get_cities($state){
if($state != NULL){
$this->db->where('state_id', $state);
$query = $this->db->get('city');
$cities = array();
if($query->result()){
foreach($query->result() as $city){
$cities[$city->id] = $city->city;
}
return $cities;
}else{
return FALSE;
}
}else{
$cities[] = "-- Select City --";
return $cities;
}
}
}
class State_model extends CI_Model{
public function __construct(){
$this -> load -> database();
}
function get_states() {
$query = $this->db->order_by('state', 'ASC')->get('state');
$states = array();
if($query -> result()){
foreach($query->result() as $state){
$states[$state -> id] = $state -> state;
}
return $states;
}else{
return FALSE;
}
}
}
<div id="innerdiv1">
<label>State</label>
<br />
<?php
$states['#'] = '-- Select State --';
echo form_dropdown('state_id', $states, '#', 'id=state_id');
?>
</div>
<div id="innerdiv2">
<label>City</label>
<br />
<div id="city">
<select name="city_id" id="city_id">
<option value="">-- Select City-- </option>
</select>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$('#states-dropdown select').change(function (){
var selState = $(this).attr('value');
console.log(selState);
$.ajax({
url: "pages/get_cities",
async: false,
type: "POST",
data: "state="+selState,
dataType: "html",
success: function(data) {
//data is the html of the page where the request is made.
$('#city').html(data);
}
})
});
});
</script>
答案 0 :(得分:0)
在评论中发布的routes.php中,更改
$route['(:any)'] = 'pages/category_list/$1';
进入
$route['pages/category_list/(:any)'] = 'pages/category_list/$1';
请注意我在代码中注意到的两件事是错的,但我不确定它们是否是原因:
1-不要使用“die”调用echo或打印数据,仅使用die进行错误
2- ajax调用应该是“GET”而不是“POST”:
$.ajax({
url: "pages/get_cities/"+selState,
async: false,
type: "GET",
success: function(data) {
//data is the html of the page where the request is made.
$('#city').html(data);
},
error: function(response) {
//Do Something on Error
}
});
答案 1 :(得分:0)
尝试这样的事情
var url = "pages/get_cities/" + $(this).attr('value');
$.ajax({
url: url,
async: false,
type: "GET",
dataType: "html",
success: function(data) {
$('#city').html(data);
}
})
php code
public function get_cities($state){
header('Content-Type: application/x-json; charset=utf-8');
echo json_encode($this->city_model->get_cities($state));
return ;
}