我创建了一个包含3个下拉列表的注册表单,每个下拉列表都与前一个下拉列表的选择有关所以我需要使用ajax但我得到的结果是第一个下拉列表工作正常但其他两个不工作它只显示默认值<choose>
我只需要修复3下拉列表,但我显示了所有的代码,让别人明白我需要什么。
<标题> register.php h1> select.class.php h1> select_village.php h1> select-district.php h1> div class="answers">在select.class.php
中最好定义两个变量,以便使用您发布的变量。如:
<?php
class SelectList {
protected $conn;
public $governorate_id;
public $district_id;
public function __construct()
{
$this->DbConnect();
}
}
?>
函数ShowDistrict()
和ShowVillage()
可以使用这些变量
用$this->varName
代替$_POST[id]
。
:
<?php
class SelectList {
protected $conn;
public $governorate_id;
public $district_id;
public function __construct()
{
$this->DbConnect();
}
public function ShowDistrict()
{
$sql = "SELECT * FROM districts WHERE governorate_id=".$this->governorate_id;
$res = mysql_query($sql,$this->conn);
var_dump($res);
$district = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$district .= '<option value="' . $row['district_id'] . '">' . $row['district_name'] . '</option>';
}
return $district;
}
public function ShowVillage()
{
$sql = "SELECT id, village_name FROM village WHERE district_id=".$this->district_id;
$res = mysql_query($sql,$this->conn);
$village = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$village .='<option value="' .$row['village_id'] . '">' . $row['village_name'] . '</option>';
}
return $village;
}
}
?>
现在你只需要在select_village.php
和select-district.php
中输入变量
select_village.php :
<?php
require_once('include/select.class.php');
$opt->governorate_id = $_POST['id'];
echo $opt->ShowVillage();
?>
select-district.php :
<?php
require_once('include/select.class.php');
$opt->district_id = $_POST['id'];
echo $opt->ShowDistrict();
?>
标题>