PHP:当更改另一个select元素中的值时,如何更改select元素的选项


PHP: How to change options in select element when changing the value in another select element?

自从上一个问题以来,我已经做了一个很大的过程。我几乎完成了我的小挑战。

现在我有另一个问题:我在html代码中有两个下拉列表(选择元素)项。在第一门课上,我可以选择一门(学校)科目。如果这个值发生变化,我希望第二个下拉列表将更新其选项,例如:

第一个下拉列表:历史第二次下降:世界大战,法国大革命

第一个下拉列表:地理第二次下降:首都,在哪里

这是我的代码:

<?php
session_start();
if(isset($_SESSION['schueler'])) {
if($_SESSION['schueler'] == 3) {
?>
<!DOCTYPE html>
<html>
<head>
<title>LA4S - Sch&uuml;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" />&nbsp;&nbsp;&nbsp;<span class="title">Sch&uuml;lerbereich</span>
</div>
<br />
<div class="menu">
    <a onclick="getquestions()">Fragen beantworten</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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&nbsp;
                </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&nbsp;
                </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>

第一个下拉列表被称为主题,第二个被称为主题

因此,我再次尝试解释:如果下拉列表subject中的值发生更改,则下拉列表主题将更新其选项。

我希望你能帮助我。

格里兹Tomi

我使用了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);
?> 

它有效!

在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>