依赖下拉列表Jquery、AJAX、PHP、MYSQL
我试图创建一个依赖下拉列表,但在我第一次选择后,它似乎没有填充。每个选择都将从依赖下拉列表Jquery、AJAX、PHP、MYSQL,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我试图创建一个依赖下拉列表,但在我第一次选择后,它似乎没有填充。每个选择都将从MySQL数据库获取数据。为了让第二个下拉菜单有任何选项(除了默认的“选择选项”值),用户必须在谷歌搜索了很多次之后,首先在第一个下拉菜单上进行选择。我很难找到一个简单的解决方案 这是我到目前为止得到的 下拉列表(我在这里使用PHP和MySQL在getter.PHP和require\u中生成下拉列表,并将其输出到index.PHP和echo下拉列表中) getfacility.php //db connection..
MySQL
数据库获取数据。为了让第二个下拉菜单有任何选项(除了默认的“选择选项”值),用户必须在谷歌搜索了很多次之后,首先在第一个下拉菜单上进行选择。我很难找到一个简单的解决方案
这是我到目前为止得到的
下拉列表(我在这里使用PHP
和MySQL
在getter.PHP
和require\u中生成下拉列表,并将其输出到index.PHP
和echo
下拉列表中)
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>";
}
//数据库连接。。
如果(!空($\u POST[“帐户id”])){
$accountID=$\u POST['account\u id'];
$sql=“从设施中选择*,account.account\u名称”
“account.account\u id=facility.account\u id上的内部加入帐户”
“WHERE facility.account_id=”、“$accountID.”;
$data=mysqli_查询($dbc,$sql);
回声“选择设施”;
而($fRow=mysqli\u fetch\u数组($data)){
$facilityOptions.=''.$fRow['facility_name'].'';
}
$facilithydropdown=“设施:
选择设施
“$facilityOptions。”
";
}
现在,当我在第一个下拉列表中进行选择时,第二个下拉列表中没有填充任何内容,我哪里出错了?在getfacility.php中进行更改
如果您的ajax在网络(控制台)中显示200 ok状态和预期响应
//数据库连接。。
如果(!空($\u POST[“帐户id”])){
$accountID=$\u POST['account\u id'];
$sql=“从设施中选择*,account.account\u名称”
“account.account\u id=facility.account\u id上的内部加入帐户”
“WHERE facility.account_id=”、“$accountID.”;
$data=mysqli_查询($dbc,$sql);
回声“选择设施”;
而($fRow=mysqli\u fetch\u数组($data)){
回显“$fRow['account_name']”;
}
我相应地更改了getfacility.php,并在控制台的“网络”选项卡下进行了检查,得到了“200 OK”状态。很好,然后在“网络->(单击文件)->响应选项卡”中进行检查“您的预期数据是否已加载?它仅输出此“选择设施”这意味着您的SQL查询没有获取此条件的数据或没有记录。我只是仔细检查了SQL查询并将其返回,它工作正常。它输出
标记的唯一原因是因为我正在返回它。
function getFacility(val) {
$.ajax({
type: "POST",
url: "getfacility.php",
data:'account_id='+val,
success: function(data){
$("#facility").html(data);
}
});
}
//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>";
}
//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>";
}