自上一个问题以来,我已经做了一个很大的过程。我几乎完成了我的小挑战。
现在我有另一个问题: 我在html代码中有两个下拉列表(选择元素)项。在第一个我可以选择(学校)科目。如果此值发生更改,我希望第二个下拉列表将更新其选项,例如:
首先下拉:历史 第二次下降:第二次世界大战,法国大革命
首先下拉:地理 第二次下拉:首都,在哪里......
这是我的代码:
<?php
session_start();
if(isset($_SESSION['schueler'])) {
if($_SESSION['schueler'] == 3) {
?>
<!DOCTYPE html>
<html>
<head>
<title>LA4S - Schülerbereich</title>
<link href="./Style/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
function getquestions() {
document.getElementById("getquestions").style.visibility = "visible";
}
</script>
<div>
<img src="./Pictures/logo_small.png" width="100" id="logo" /> <span class="title">Schülerbereich</span>
</div>
<br />
<div class="menu">
<a onclick="getquestions()">Fragen beantworten</a>
<a style="text-decoration: none;" href="login.php?logout=3">Ausloggen</a>
</div>
<br />
<div id="getquestions">
<table>
<form action="home.php" method="post">
<tr>
<td>
Fach
</td>
<td>
<select name="subject" size="1" id="type_select">
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbase = "la4s";
$db = mysqli_connect($host, $user, $pass, $dbase);
if (mysqli_connect_errno())
{
echo mysqli_connect_errno();
die ("Error");
}
$getSubjectsForForm = mysqli_query($db, "SELECT * FROM subject");
while($ResultArray = mysqli_fetch_array($getSubjectsForForm)) {
echo '<option value="'.$ResultArray['sid'].'">'.$ResultArray['subject'].'</option>';
}
mysqli_close($db);
?>
</select>
</td>
</tr>
<tr>
<td>
Thema
</td>
<td>
<select name="theme" size="1" id="type_select">
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbase = "la4s";
$db = mysqli_connect($host, $user, $pass, $dbase);
if (mysqli_connect_errno())
{
echo mysqli_connect_errno();
die ("Error");
}
$subject = $_POST['subject'];
$getThemesOfThatSubject = mysqli_query($db, "SELECT * FROM theme WHERE sid =".$subject.";");
while($ResultArray = mysqli_fetch_array($getThemesOfThatSubject)) {
echo '<option value="'.$ResultArray['tid'].'">'.$ResultArray['theme'].'</option>';
}
mysqli_close($db);
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" id="button_login">
<input type="submit" name="submit" value="Fragen beantworten" />
</td>
</tr>
</form>
</table>
</div>
<?php
}
else {
echo "Zugriff verweigert!";
}
}
else {
echo "Zugriff verweigert!";
}
?>
</body>
</html>
第一个下拉列表称为主题,第二个下拉列表称为主题。
所以我再次尝试解释:如果下拉列表主题中的值发生变化,下拉列表主题将更新其选项。
我希望你能帮助我。
Greeez 托米
答案 0 :(得分:1)
我用SHAKIR SHABBIR的解决方案做到了。
<强>的Javascript 强>
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(新文件/网站)
<?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);
?>
有效!
答案 1 :(得分:0)
在PHP代码中
<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>