我花了一些根據您的問題寫一些代碼。在寫這篇文章的時候,我假設你在存儲類別和選項的兩個表格之間有關係。我假定關係使用「Gradelvl_ID」。我還假設你在JavaScript
,JQuery
和AJAX
有一些知識。
因此,基於我創建了下面的代碼。
這將是您的選擇區域。
<hmtl>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
</head>
<body>
<label>Section</label>
<select class="form-control selcls" name="txtsection" id="cat" >
<?php
while ($rows = mysqli_fetch_array($queryResultsec)) { ?>
<option id="<?php echo $rows['Gradelvl_ID'];?>"><?php echo $rows['Section_Name'];?></option>
<?php } ?>
</select>
<label>Section</label>
<select class="form-control selcls" name="txtsection" id="options" ></select>
</body>
</html>
此腳本使用jQuery,因此您需要將jQuery庫鏈接到上面的頁面。你也可以在第一頁使用<script></script>
標籤或者作爲.js
文件拼寫這個腳本。
$(document).ready(function(){
$(document).on('change', '#cat', function(){
$.ajax({
url: 'getOptions.php',
type: 'get',
data: {
catId: $(this).prop('id')
}
}).then(function (response) {
$('#options').html(response);
});
});
})
上面的代碼將選定的ID發送到getOptions.php其中將包含PHP
根據從您選擇表發送ID號選擇的所有選項。然後,如果選擇成功,它將發回數據,這些數據將被上述AJAX
代碼捕獲,並在第二個下拉列表中繪製選項。
<?php
include_once('dbconnect.php');
//I'm not a big mysqli user
if(!empty($_GET["id"])){
$results = $conn -> prepare("SELECT * FROM <your table name> WHERE id = ?");
$results -> bind_param('i', $_GET["id"]);
$results -> execute();
$rowNum = $results -> num_rows;
if ($rowNum > 0){
while($optRows = $results -> fetch_assoc()){ ?>
<option id="<?php echo $rows['Gradelvl_ID'];?>"><?php echo $rows['Section_Name'];?></option>
<?php
}
}
}?>
另外要注意上面的代碼我使用預處理語句非常好習慣中來關注一下吧here
正如我說我是假設的代碼的某些部分,並使用您和我提供的信息希望您能做更多的研究並使上述代碼適合您。
如果這個工程,我想請你給我投票了,並標記這個答案作爲問題選擇的答案。
提供的代碼你試過@Jeremy克魯茲 –
你可以使用AJAX它。 – javidrathod
你是否使用數據庫所以顯示你的代碼來幫助 –