im將數據庫中的主題列表打印到下拉列表中。當選擇一個主題時,下一個名爲section的下拉菜單應該通過在數據庫中查找主題名稱來顯示主題的各個部分。在下面的代碼中使用下拉式鏈接
這是我能夠做的,但它沒有工作,任何幫助的人,請忽略我的編碼風格,因爲我是初學者。先謝謝你。
主要代碼:
<div>
Subject:
<?php
$conn = new mysqli('fgdfgfg', 'fggfdgfr', 'fgfgf', 'fgfgfgf') or die('Cannot connect to db');
$result = $conn->query("select name from class");
echo "<select name='subject' nChange='getSection('findsection.php?subject='+this.value)'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$name = $row['name'];
echo '<option value="subject">' . $name . '</option>';
}
echo "</select>";
?>
</div>
<br>
<div id="sectiondiv">
Section:
<select name="select">
</select>
</div>
findsection.php
<? $subject=intval($_GET[‘subject’]);;
$servername = "localhost";
$username = "fhdfhdfhf";
$password = "ghhfghgh";
$dbname = "ghghgh";
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$section = $mysqli->query("SELECT section FROM class WHERE name = '$subject'")->fetch_object()->section;
$result=mysql_query($section);?>
<select name="section">
<? while ($row = $result->fetch_assoc()) { ?>
<option value><?=$row['section']?></option>
<? } ?>
</select>
JS代碼:
<script type="text/javascript">
function getSection(strURL)
{
var req = getXMLHTTP(); // fuction to get xmlhttp object
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) { //data is retrieved from server
if (req.status == 200) { // which reprents ok status
document.getElementById('sectiondiv').innerHTML=req.responseText;
}
else
{
alert("There was a problem while using XMLHTTP:\n");
}
}
}
req.open("GET", strURL, true); //open url using get method
req.send(null);
}
}
</script>
使它正確nChange ='getSection它將onchange ='getSection – Saty
你可以更清楚地知道你的代碼在什麼時候停止工作。是的,也許javascript函數沒有被調用,或者可能是XHR,甚至是數據庫錯誤。你必須想出調試方法。 (提醒控制檯日誌響應..螢火蟲工具等,..) – animaacija
@saty固定,但仍然代碼將無法正常工作。 – Sam