繁体   English   中英

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

[英]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>

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

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

我希望你能帮助我。

格里兹·托米(Greeez 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM