0
我有這個文件名爲ajax.php 'AJAX,PHP和jQuery下拉
<?php
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 = "dd";
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 country GROUP BY Name";
$this->_fields = array('Code','Name');
break;
case 'state':
$this->_query = "SELECT * FROM city WHERE CountryCode=$id";
$this->_fields = array('CountryCode','District');
break;
case 'city':
$this->_query = "SELECT * FROM city WHERE CountryCode=$id";
$this->_fields = array('CountryCode','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;
?>
和index.html這個JS腳本
和形式
<form>
<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="loader.gif" id="loading" align="absmiddle" style="display:none;"/>
</form>
國家選項可見但是當我想選擇它不是的狀態時
另外我有兩個表cou ntry和城市,並在表城市包括區名
如果是'load_options'的代碼? – Charx
我不能把它作爲js代碼來引入,這是用什麼標籤? – user2036948
介紹爲[CODE] [/ CODE] –