PHP:当更改另一个select元素中的值时,如何更改select元素中的选项?
自从上一个问题以来,我已经做了一个大的过程。我几乎完成了我的小挑战 现在我有另一个问题: 我有两个下拉列表选择html代码中的元素和项目。在第一个选项中,我可以选择一个学校科目。如果此值更改,我希望第二个下拉列表将更新其选项,例如: 第一个下拉列表:历史 第二次失败:世界大战、法国大革命 第一个下拉列表:地理 第二个下拉列表:大写,哪里是 这是我的密码: 第一个下拉列表称为主题,第二个下拉列表称为主题 因此,我再次尝试解释:如果下拉列表主题中的值发生变化,下拉列表主题将更新其选项 我希望你能帮助我 格里兹 PHP代码中的TomiPHP:当更改另一个select元素中的值时,如何更改select元素中的选项?,php,Php,自从上一个问题以来,我已经做了一个大的过程。我几乎完成了我的小挑战 现在我有另一个问题: 我有两个下拉列表选择html代码中的元素和项目。在第一个选项中,我可以选择一个学校科目。如果此值更改,我希望第二个下拉列表将更新其选项,例如: 第一个下拉列表:历史 第二次失败:世界大战、法国大革命 第一个下拉列表:地理 第二个下拉列表:大写,哪里是 这是我的密码: 第一个下拉列表称为主题,第二个下拉列表称为主题 因此,我再次尝试解释:如果下拉列表主题中的值发生变化,下拉列表主题将更新其选项 我希望你能帮助
<select name="subject" size="1" id="type_select" onchange="updateThemeDropdown(this)">
/*
Retrieve subject list from PHP
*/
</select>
<select name="theme" size="1" id="type_select">
/*
Keep it empty and fill it from updateThemeDropdown(this) in Javascript
*/
</select>
在Javascript代码中
<script type="text/javascript">
function updateThemeDropdown(dropdownObject) {
/*
1- Read the dropdownObject and get the selected option
2- Pass the selected option to another PHP Page that returns list of themes for the selected object via AJAX call
3- Parse the returned object from AJAX call and set the options for "theme" dropdown
You should research doing all this.
Use JQuery for these steps
*/
}
</script>
我用夏吉尔·沙比尔的解决方案做到了 Javascript PHP新文件/站点
它起作用了 要做到这一点,您需要一些ajax和jquery。您必须为第一个下拉列表编写onchange事件处理程序,然后在第二个下拉列表中操作选项。我在实现您的解决方案时遇到问题。我是怎么读dropdownobject的?我有。谢谢你的帮助。我将在新答案中发布我的解决方案。非常感谢你。
<script type="text/javascript">
function updateThemeDropdown(dropdownObject) {
/*
1- Read the dropdownObject and get the selected option
2- Pass the selected option to another PHP Page that returns list of themes for the selected object via AJAX call
3- Parse the returned object from AJAX call and set the options for "theme" dropdown
You should research doing all this.
Use JQuery for these steps
*/
}
</script>
function updateThemeDropdown(str) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("divTheme").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getthemes.php?q="+str,true);
xmlhttp.send();
}
<?php
$q = intval($_GET['q']);
$host = "localhost";
$user = "root";
$pass = "";
$dbase = "la4s";
$db = mysqli_connect($host, $user, $pass, $dbase);
if (mysqli_connect_errno())
{
echo mysqli_connect_errno();
die ("Error");
}
$sql="SELECT * FROM theme WHERE sid = '".$q."'";
$getThemesOfThatSubject = mysqli_query($db,$sql);
echo '<select name="theme" size="1" id="type_select">';
while($ResultArray = mysqli_fetch_array($getThemesOfThatSubject)) {
echo '<option value="'.$ResultArray['tid'].'">'.$ResultArray['theme'].'</option>';
}
echo '</select>';
mysqli_close($db);
?>