我正在尝试创建一个依赖的下拉列表,但是在我进行第一次选择后似乎没有填充。每个选择将从MySQL
数据库中获取数据。为了使第二个下拉菜单具有任何选项(默认的“选择选项”值除外),用户必须首先在第一个下拉菜单上进行选择。经过大量的搜索之后,我很难找到一个简单的选项解决方案。
这是我到目前为止的一切
下拉列表(我使用PHP
,并MySQL
在这里产生并输出一个下拉列表getter.php
和require_once
成index.php
和echo
出下拉)
$accountOptions = "";
$facilityOptions = "";
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
die("Connection failed: " . mysqli_connect_error());
}
///ACCOUNTS/////
$accountQuery = "SELECT account_id, account_name FROM account";
$accountData = mysqli_query($dbc, $accountQuery);
//loop through data and display all accounts
while ($aRow = mysqli_fetch_array($accountData)) {
$accountOptions .="<option value=\"".$aRow['account_id']."\">" . $aRow['account_name'] . "</option>";
}
$accountDropDown=" <label>Accounts: </label><br>
<select name='account' id='account' onChange='getFacility(this.value)'>
<option selected='selected' disabled='disabled' value=''>Select account</option>
" . $accountOptions . "
</select>";
////FACILITIES/////
$facilityDropDown=" <label>Facility: </label><br>
<select name='facility' id='facility'>
<option selected='selected' disabled='disabled' value=''>Select facility</option>
</select>";
jQuery / AJAX
function getFacility(val) {
$.ajax({
type: "POST",
url: "getfacility.php",
data:'account_id='+val,
success: function(data){
$("#facility").html(data);
}
});
}
getfacility.php
//db connection..
if(!empty($_POST["account_id"])) {
$accountID = $_POST['account_id'];
$sql = "SELECT *, account.account_name FROM facility "
. "INNER JOIN account ON account.account_id = facility.account_id "
. "WHERE facility.account_id = '". $accountID ."'";
$data = mysqli_query($dbc, $sql);
echo "<option selected='selected' disabled='disabled' value=''>Select facility</option>";
while ($fRow = mysqli_fetch_array($data)) {
$facilityOptions .="<option value=\"".$fRow['facility_id']."\">" . $fRow['facility_name'] . "</option>";
}
$facilityDropDown=" <label>Facility: </label><br>
<select name='facility' id='facility'>
<option selected='selected' disabled='disabled' value=''>Select facility</option>
" . $facilityOptions . "
</select>";
}
现在,当我在第一个下拉列表中进行选择时,第二个下拉列表中没有任何内容,我在哪里出错?
在getfacility.php中进行更改,
如果您的ajax在“网络”(控制台)中显示200 ok状态和预期响应
//db connection..
if(!empty($_POST["account_id"])) {
$accountID = $_POST['account_id'];
$sql = "SELECT *, account.account_name FROM facility "
. "INNER JOIN account ON account.account_id = facility.account_id "
. "WHERE facility.account_id = '". $accountID ."'";
$data = mysqli_query($dbc, $sql);
echo "<option selected='selected' disabled='disabled' value=''>Select facility</option>";
while ($fRow = mysqli_fetch_array($data)) {
echo "<option value=\"".$fRow['account_id']."\">" . $fRow['account_name'] . "</option>";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句