我收集国家和州的代码,我从选择标签中选择一个国家名称,并在国家下所有州显示在下一个选择框中。所有数据均来自 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>