我收集国家和州的代码,我从select标签中选择一个国家/地区名称,然后在下一个选择框中的国家/地区下显示所有州。所有数据都来自MySQL,由jQuery,ajax处理。
我无法从控制器获得价值。为什么?
$data=$this->input->post('state',true)
我的代码如下:模型中的CI
<?php
/* File : ajax.php
* Author : Manish Kumar Jangir
*/
class AJAX {
private $database = NULL;
private $_query = NULL;
private $_fields = array();
public $_index = NULL;
const DB_HOST = "localhost";
const DB_USER = "root";
const DB_PASSWORD = "";
const DB_NAME = "csc";
public function __construct(){
$this->db_connect(); // Initiate Database connection
$this->process_data();
}
/*
* Connect to database
*/
private function db_connect(){
$this->database = mysql_connect(self::DB_HOST,self::DB_USER,self::DB_PASSWORD);
if($this->database){
$db = mysql_select_db(self::DB_NAME,$this->database);
} else {
echo mysql_error();die;
}
}
private function process_data(){
$this->_index = ($_REQUEST['index'])?$_REQUEST['index']:NULL;
$id = ($_REQUEST['id'])?$_REQUEST['id']:NULL;
switch($this->_index){
case 'country':
$this->_query = "SELECT * FROM countries";
$this->_fields = array('id','country_name');
break;
case 'state':
$this->_query = "SELECT * FROM states WHERE country_id=$id";
$this->_fields = array('id','state_name');
break;
case 'city':
$this->_query = "SELECT * FROM cities WHERE state_id=$id";
$this->_fields = array('id','city_name');
break;
default:
break;
}
$this->show_result();
}
public function show_result(){
echo '<option value="">Select '.$this->_index.'</option>';
$query = mysql_query($this->_query);
while($result = mysql_fetch_array($query)){
$entity_id = $result[$this->_fields[0]];
$enity_name = $result[$this->_fields[1]];
echo "<option value='$entity_id'>$enity_name</option>";
}
}
}
$obj = new AJAX;
?>
这是我的观点:
<html>
<head>
<title>Country State City Dependent Dropdown using Ajax</title>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-1.5.2.min.js"></script>
<style type="text/css">
body{
font-family:arial;
color:#666;
}
form select{
padding:5px;
border:1px solid #aaa;
border-radius:4px;
width:120px;
}
form label{
font-size:12px;
font-weight:bold;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
load_options('','country');
});
function load_options(id,index){
$("#loading").show();
if(index=="state"){
$("#city").html('<option value="">Select city</option>');
}
$.ajax({
url: "<?php echo base_url(); ?>index.php/welcome/includ.html?index="+index+"&id="+id,
complete: function(){$("#loading").hide();},
success: function(data) {
$("#"+index).html(data);
}
})
}
</script>
</head>
<body>
<div style="width:800px; margin:auto;padding-top:100px;">
<h1>Country,State,City dynamic dependent dropdown using Ajax and Jquery</h1>
<form action="<?php echo base_url(); ?>index.php/city_ajax/show_result" method="post">
<label>Select Country</label>
<select id="country" onchange="load_options(this.value,'state');">
<option value="">Select country</option>
</select>
<label>Select State</label>
<select id="state" onchange="load_options(this.value,'city');">
<option value="">Select state</option>
</select>
<label>Select city</label>
<select id="city">
<option value="">Select City</option>
</select>
<img src="<?php echo base_url(); ?>loader.gif" id="loading" align="absmiddle" style="display:none;"/>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
SELECT语句:
country_id=$id
应为country_id='" . $id . "'
state_id=$id
应为state_id='" . $id . "'
答案 1 :(得分:0)
正如你所说的CI模式:
CodeIgniter中的您必须将CI_Model扩展为:
class Exam_model extends CI_Model {}
为了使其成为CI模型,它只是一个.php文件
并且
关于这个
$data=$this->input->post('state',true);
您正在尝试评估一个帖子参数,该参数需要在ajax中输入作为POST
url: "<?php echo base_url(); ?>index.php/welcome/includ.html?index="+index+"&id="+id,
complete: function(){$("#loading").hide();},
type:"POST", // you have to mention post type here
success: function(data) {
$("#"+index).html(data);
}
希望它会有所帮助!