我的动态相关选择框出了问题,我从早上起就试图解决它,但我还是无法解决它。第一个选择框从数据库中获取所有区域,但第二个选择框不响应第一个值并且不返回任何内容,我真的不知道为什么?我检查了一切,包括我的表格,我的PHP代码,我的数据库和我的脚本,但仍然不知道发生了什么。有什么帮助吗?
这是我的表格
<form action="" enctype="multipart/form-data" method="post">
<div class="grid-2">
<div>
<div class="line" style="width: 105%;">
<label class="label" for="regionId">Region *</label>
<div class="selectWrapper">
<select class="select " id="region" name="regionId">
<option value="">Sélectionner la Region</option>
<?php
$getRegion = $region->getAllRegion();
if($getRegion){
while($result = $getRegion->fetch_assoc()){
?>
<option value="<?php echo $result['regionId']; ?>"><?php echo $result['regionName']; ?></option>
<?php } } ?>
</select>
</div>
</div>
</div>
<div>
<div class="line" style="width: 105%;">
<label class="label" for="townId">Ville *</label>
<div class="selectWrapper">
<select class="select" id="town" name="townId">
<option value="">Select Region first</option>
</select>
</div>
</div>
</div>
</div>
<div class="line">
<label class="label" for="phone">Téléphone *</label>
<input type="text" class="input full" name="phone" value=""/>
</div>
<div class="line">
<label class="label" for="Email">Adresse email *</label>
<input type="text" class="input full" name="email" value="" />
</div>
<center><input type="submit" id="create_store_button" class="button-blue full mtm" value="Reserver"></center>
这是我的Ajax Fetch.php
<?php
include '../lib/session.php';
include '../lib/database.php';
include '../helpers/format.php';
include '../helpers/format2.php';
spl_autoload_register(function($class){
include_once "../classes/".$class.".php";
});
$db = new Database();
$fm = new Format();
$region = new Region();
if(!empty($_POST["regionId"])){
$getTown = $region->getAllTown();
if($getTown){
echo '<option value="">Select Town</option>';
while($result = $getTown->fetch_assoc()){
echo '<option value="'.$result['townId'].'">'.$result['townName'].'</option>';
}
}else{
echo '<option value="">Town not available</option>';
}
}
?>
这是我的Javascript
<script>
$(document).ready(function(){
$('#region').on('change',function(){
var regionID = $(this).val();
if(regionID){
$.ajax({
type:'POST',
url:'fetch.php',
data:'regionId='+regionID,
success:function(html){
$('#town').html(html);
}
});
}else{
$('#town').html('<option value="">Select Region first</option>');
}
});
</script>
这是我的PHP类
<?php
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/../lib/database.php');
include_once ($filepath.'/../helpers/format.php');
?>
<?php
class Region{
private $db;
private $fm;
public function __construct(){
$this->db = new Database();
$this->fm = new Format();
}
public function getAllRegion(){
$query = "SELECT * FROM region ORDER BY regionId";
$result = $this->db->select($query);
return $result;
}
public function getAllTown(){
$query = "SELECT * FROM town WHERE regionId = ".$_POST['regionId']." ORDER BY townName ASC";
$result = $this->db->select($query);
return $result;
}
}
?>