我一直在使用PHP作为服务器端语言和使用Ajax的JQuery的依赖选择框表单。我在获取响应文本时遇到问题,因为它没有在第二个选择框中显示为选项。
P.S。我是Ajax的新手,没有视频可以帮助我解决我的问题。
HTML&安培; PHP:
<center><form method="post" action="php/functions.php" id="catForm">
<select name="catSelect" class="catSelect" name="category">
<option value='null' default>اختر الفئة:</option>
<?php
$selectCategories = mysqli_query($connectionDB, "SELECT * FROM categories");
while($categoriesDisplay = mysqli_fetch_array($selectCategories)){
echo '<option value="'.$categoriesDisplay['id'].'">'.$categoriesDisplay['category'].'</option>';
}
?>
</select><br/><br/>
<select name="subCatSelect" class="subCatSelect">
<option value="null" default>اختر النوع:</option>
<?php
$catSelectVal = $_POST['catSelect'];
$selectSubCat = mysqli_query($connectionDB, "SELECT * FROM sub_categories WHERE id LIKE '$catSelectVal'");
while($subCatDisplay = mysqli_fetch_array($selectSubCat)){
echo '<option value="'.$subCatDisplay['id'].'">'.$subCatDisplay['subCategory'].'</option>';
}
?>
</select><br/>
<h1></h1>
<input type="submit" value="اختر" class="submitForm" /><br/>
</form></center>
Jquery代码:
$(document).ready(function(){
$('.catSelect').change(function(){
var changeURL = $('#catForm').attr("action");
var data = $('.catSelect').val();
$.post(changeURL, {category : data}, function(subCategory){
$('.subCatSelect').append(subCategory);
});
});
});
应该用于获取第二个选择框的选项的代码:
$catSelectVal = $_POST['catSelect'];
$selectSubCat = mysqli_query($connectionDB, "SELECT * FROM sub_categories WHERE id LIKE '$catSelectVal'");
while($subCatDisplay = mysqli_fetch_array($selectSubCat)){
echo '<option value="'.$subCatDisplay['id'].'">'.$subCatDisplay['subCategory'].'</option>';
}
答案 0 :(得分:0)
我可能是错的,但我认为你没有在ajax请求中发送'catSelect'
..而是你已经category
发送了data
'catSelect'
来自$catSelectVal = $_POST['catSelect'];
因此,当您寻找$catSelectVal = $_POST['category'];
时,无法找到任何东西。
请改为:print_r($_POST);
正如@RiggsFolly所提到的,如果你"aggs" : {
"sum_of_different_buckets" : { "sum" : { "field" : "docs" } }
}
,你会立即看到是否是这种情况。