我一直在想方設法從數據庫中填充第二個下拉列表,這取決於用戶在第一個下拉列表中的選擇。使用數據庫進行動態下拉列表
到目前爲止,CSS Tricks (Dynamic-Dropdowns)這是對我的問題最好和最明確的答案。雖然我無法開展工作。 (有3個例子來填充下拉列表,你應該檢查數據庫之一,這是在頁面的底部。)
我有2個下拉框在我的settings.php和教程顯示我創建了另一個php文件打印第二下拉。
這是獲得-dropdown.php:
<script>alert("Here")</script>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$dbConnection = open_connection();
if(isset($_GET['School'])){ $school = mysqli_real_escape_string($dbConnection, $_GET['School']); }
/* This code will print program options from database.
*
* If user's program matches with any of the school from database,
* mark it as "selected" otherwise, use "Select Your Program" as selected.
*
* So, "selected" attribute of user's program will overwrite the "selected"
* attribute of "Select Your Program".
* */
$query_programs = "SELECT * FROM PROGRAMS WHERE PROGRAM_SCHOOL='$school' ORDER BY PROGRAM_CODE ASC";
$query_users = "SELECT USER_PROGRAM FROM USERS WHERE USER_ID = $user1_id";
$programs_result = mysqli_query($dbConnection, $query_programs) or die(mysqli_error($dbConnection));
$users_result = mysqli_query($dbConnection, $query_users) or die(mysqli_error($dbConnection));
while($data = mysqli_fetch_assoc($users_result)){ $user_program = $data['USER_PROGRAM']; }
foreach($programs_result as $program_result){
if($user_program == $program_result['PROGRAM_CODE']){
echo "<option value='$program_result[PROGRAM_CODE]' selected>$program_result[PROGRAM_CODE]</option>";
}else{
echo "<option value='$program_result[PROGRAM_CODE]'>$program_result[PROGRAM_CODE]</option>";
}
}
close_connection($dbConnection);
即使是在頂層警告不起作用。我把它放在那裏看看它是否進入這個頁面。當我從第一個下拉列表中選擇另一個選項時,第二個下拉列表變爲空。什麼都沒有出現看起來我在settings.php中犯了一個錯誤,因爲警報不起作用。
這是我的settings.php的某些部分:
<label>
<span>School:</span>
<select class="settings-input" name="school" id="school">
<option value="Select Your School" disabled selected>Select Your School</option>
<?php
/* This code will print school options from database.
*
* If user's school matches with any of the school from database,
* mark it as "selected" otherwise, use "Select Your School" as selected.
*
* So, "selected" attribute of user's school will overwrite the "selected"
* attribute of "Select Your School".
* */
$query_schools = "SELECT * FROM SCHOOLS ORDER BY SCHOOL_TYPE ASC";
$query_users = "SELECT USER_SCHOOL FROM USERS WHERE USER_ID = $user1_id";
$schools_result = mysqli_query($dbConnection, $query_schools);
$users_result = mysqli_query($dbConnection, $query_users);
while($data = mysqli_fetch_assoc($users_result)){ $user_school = $data['USER_SCHOOL']; }
foreach($schools_result as $school_result){
if($user_school == $school_result['SCHOOL_NAME']){
echo "<option value='$school_result[SCHOOL_NAME]' selected>$school_result[SCHOOL_NAME]</option>";
}else{
echo "<option value='$school_result[SCHOOL_NAME]'>$school_result[SCHOOL_NAME]</option>";
}
}
?>
<option value="Other">Other</option>
</select>
</label>
<label>
<span>Program:</span>
<select class="settings-input" name="program" id="program">
<option value="Select Your Program" disabled selected>Select Your Program</option>
<script>
$("#school").change(function(){
$("#program").load("./lib/get-dropdown.php?school=" + $("#school").val());
});
</script>
</select>
</label>
非常感謝你。
最後固定(我的解決方案) 1.我有$的DbConnection = OPEN_CONNECTION();連接數據庫,但此功能在另一個文件中定義,連接數據庫所需的信息存儲在另一個文件中。所以,對我的get-dropdown.php我必須要求這兩個文件。所以這是我如何修復數據庫連接。 2.其他問題是我將學校名稱傳遞給get-dropdown.php,但問題是學校名稱包含空格,當您嘗試傳入get時,這是個問題。所以這就是我曾經傳遞的價值。我加了encodeURIComponent。
<script>
$(document).ready(function(){
$("#school").change(function(){
$("#program").load("lib/get-dropdown.php?School=" + encodeURIComponent($("#school").val()));
});
});
</script>
這些都是問題所在。如果您嘗試填充下拉列表並且不瞭解JavaScript,則這是最簡單的方法。有了一點點jQuery,你可以實現它。
您正在'mysql_'函數中加入'mysqli_',*爲什麼?* –
加上,變量/ GET數組區分大小寫,另一個貼在您的前面(自行車)輪 –
我現在修復了。我從網站複製了該部分並對其進行了修改。那裏沒有意識到mysql。現在它是固定的。但下拉菜單仍然無效。 – cyonder