我花了一些根据您的问题写一些代码。在写这篇文章的时候,我假设你在存储类别和选项的两个表格之间有关系。我假定关系使用“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
你是否使用数据库所以显示你的代码来帮助 –