Javascript 使用ajax和php将数据库值传递到select

Javascript 使用ajax和php将数据库值传递到select,javascript,php,html,mysql,ajax,Javascript,Php,Html,Mysql,Ajax,我想从数据库中获取数据,并将其显示在下拉列表中。所以当我点击这个选项时,它应该会给我这个数据库的结果。因此,如果我在数据库中手动添加一个新条目,我的选择选项应该会自动变大 这是我的密码: index.html: <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <script>

我想从数据库中获取数据,并将其显示在下拉列表中。所以当我点击这个选项时,它应该会给我这个数据库的结果。因此,如果我在数据库中手动添加一个新条目,我的选择选项应该会自动变大

这是我的密码: index.html:

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
        <script>
            function showUser(str) {
                if (str == "") {
                    document.getElementById("txtHint").innerHTML = "";
                    return;
                } else {
                    var xmlhttp = new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function() {
                        if (this.readyState == 4 && this.status == 200) {
                            document.getElementById("txtHint").innerHTML = this.responseText;
                        }
                    };
                    xmlhttp.open("GET","getuser.php?q="+str,true);
                    xmlhttp.send();
                }
            }
        </script>
    </head>
<body>

<form>
    <select name="users" onchange="showUser(this.value)">
        <option value="">Select a person:</option>
        <option value="1">here should be opt1 from db</option>
        <option value="2">here should be opt1 from db</option>
        <option value="3">here should be opt1 from db</option>
        <option value="4">here should be opt1 from db</option>
    </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

getuser.php


<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

        th {text-align: left;}
    </style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','ajax');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax");
$sql="SELECT * FROM users WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Alter</th>
<th>Stadt</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['vorname'] . "</td>";
    echo "<td>" . $row['nachname'] . "</td>";
    echo "<td>" . $row['r_alter'] . "</td>";
    echo "<td>" . $row['stadt'] . "</td>";
    echo "<td>" . $row['job'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);


?>
</body>
</html>
我需要更改代码,以便在启动时用当前值动态填充select字段​​从数据库中

我希望你能帮我


谢谢大家!

我理解你问题的方式:

JavaScript:在标记之前添加此脚本

选择选项的功能将变大{ $.ajax{ 键入:“GET”, url:'selectlist.php', 成功:函数结果{ $growtbiger.htmlresult; } }; } $document.readyfunction{ 设置间隔函数{ 选择权的功能越来越大; },3000;//3秒 }; HTML:addid=1

选择一个人: 这里应该是db的opt1 这里应该是db的opt1 这里应该是db的opt1 这里应该是db的opt1 现在创建selectlist.php并使用php代码在select选项中获取数据。然后刷新页面,每3秒自动重新生成下拉列表。您可以在数据库中手动添加选项。

请阅读。如果你想寻求我们的帮助,那么你需要具体说明你真正需要帮助的是什么。显然,您已经知道如何从数据库中选择内容,以及如何循环生成的记录。那么,你现在的问题到底在哪里呢?a选择你需要的数据,然后b在循环中从该数据中创建选项元素?