简体   繁体   English

如何从选择列表中选择选项,然后根据数据库记录更改第二个选择列表?

[英]How to select option from select list, that then changes the second select list, depending on database records?

I have a form with several select drop down lists. 我有一个带有几个选择下拉列表的表单。 The first option should be selected (Summer, Spring or Winter) and the second drop-down list will then show the students attending classes in this period. 应该选择第一个选项(夏季,春季或冬季),然后第二个下拉列表将显示在此期间上课的学生。

I am currently using a PHP variable (which holds the first selection) in the sql request to the PHP - this may be my problem, but I can't see a solution for the life of me. 我目前在对php的sql请求中使用一个PHP变量(保留第一个选择)-这可能是我的问题,但我看不到我的生活的解决方案。

I'm open to all ideas. 我愿意接受所有想法。 I've looked at a few similar problems already on StackOverflow but none seem to fit my problem. 我已经在StackOverflow上查看了一些类似的问题,但似乎没有一个适合我的问题。

<div class="form-group col-md-12">
    <select name="classe" id="classe" class="form-control">
        <option value="">Sélectionnez la classe</option>
        <?php
        while ($rows = $result->fetch_assoc()) {
            $classe = $rows['classe'];
            echo "<option value='$classe'>$classe</option>";
        }
        ?>
    </select>
</div>
<div class="form-group col-md-12">
    <select name="etudiant" id="etudiant" class="form-control">
        <option value="">Sélectionnez l'étudiant'</option>
        <?php
        while ($rows3 = $result3->fetch_assoc()) {
            $etudiant = $rows3['name'];
            echo "<option value='" . $rows3['id'] . "'>$etudiant</option>";
        }
        ?>
    </select>
</div>


$classe = filter_input(INPUT_POST, "classe");
$askEtudiant = new mysqli('localhost', 'root', '', 'attendance');
$result3 = $askEtudiant->query("SELECT id, name FROM users WHERE role = 'etudiant' AND classe = '$classe'");

I haven't included the conncetion info because I can see that works. 我没有包括转换信息,因为我可以看到它的工作原理。

The first select list is called "classe" and has the time of year the students are on campus. 第一个选择列表称为“班级”,具有学生一年中在校园中的时间。

The second list "etudiant should list those students in the database that are registered for the period selected previously. 第二个列表“学生应列出数据库中先前选定时期内已注册的那些学生。

FOR INFO: The results I currently get back in the second drop down list is the one student "etudiant" that doesn't have a period (Summer, etc) assigned to him in the database! 对于信息:我目前在第二个下拉列表中返回的结果是在数据库中没有为其分配时段(夏季等)的一名学生“学生”!

Thanks in advance for any help... 预先感谢您的任何帮助...

Its called cascading dropdowns. 它称为级联下拉列表。

Basically, you will want to fire an AJAX call on the onChange event of the first dropdown and populate the second dropdown from the API response. 基本上,您将希望在第一个下拉列表的onChange事件上触发AJAX调用,并从API响应中填充第二个下拉列表。

SOLUTION SCRIPT IN HTML FILE HTML文件中的解决方案脚本
function showUser(str) { if (str == "") { document.getElementById("etudiant").innerHTML = ""; 函数showUser(str){if(str ==“”){document.getElementById(“ etudiant”)。innerHTML =“”; return; 返回; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } if(window.XMLHttpRequest){// IE7 +,Firefox,Chrome,Opera,Safari的代码xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } else {// IE6,IE5的代码xmlhttp = new ActiveXObject(“ Microsoft.XMLHTTP”); } xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("etudiant").innerHTML = this.responseText; } xmlhttp.onreadystatechange = function(){if(this.readyState == 4 && this.status == 200){document.getElementById(“ etudiant”)。innerHTML = this.responseText; } } xmlhttp.open("GET", "getUsers.php?q=" + str, true); }} xmlhttp.open(“ GET”,“ getUsers.php?q =” + str,true); xmlhttp.send(); xmlhttp.send(); } }

SEPERATE FILE 
<?php
require_once("connection.php");
$id =  $_GET["q"];

$result3 = "SELECT id, name FROM users WHERE classe = :classe";
$stmt = $conn->prepare($result3);
$stmt->bindParam(":classe", $_GET['q']);
$stmt->execute();
$users = $stmt->fetchAll();
echo "<option value=\"\">Sélectionnez l'étudiant'</option>";

foreach ($users as $key => $value) {
echo "<option value='" . $value['id'] . "'>" . $value['name'] . "</option>";
}

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

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