MySQLi选择列表取决于所选的上一个


MySQLi Select list dependent on the selected previous

所以我有一个包含2个表的数据库和一个包含两个选择列表的页面。根据第一次选择时选择的值,我想在第二次选择时显示相应的项目。

没有任何错误或任何东西,但第二个选择从来没有显示任何东西,所以我可能错过了一些东西。有人能帮我吗?

连接.php

    <?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dberror1 = "Could not connect to the database";
$dberror2 = "Could not find the table";
$Conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$Select_db = mysqli_select_db($Conn, 'swimming') or die ($dberror2);
?>

结果.php

    <?php
include ("Connections/connection.php");
$query_comp = mysqli_query($Conn, "Select * FROM comp");
?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>Swimming Results</title>
<link href="file:///D|/Websites/Swimming/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
    <!-- Div for Title -->
    <div id="title">    
        <p>Swimming Results</p>
    </div>

<form name="results"    > 
    <!-- Div to Choose competition -->
    <div id="competition">
        <p>Competition</p>

      <select name="sel_comp">
        <option value="">---Select one competition---</option>
    <?php
    while ($fetch = mysqli_fetch_assoc($query_comp)){
        echo "<option value='{".$fetch['id']."}'>".$fetch['comp_name']."</option>";
}

?>
</select>


  </div>

    <!-- Div to Chose event -->
    <div id="event">
        <p>Event</p>

        <select name="sel_even">
        <option value="">---Select one event---</option>
    <?php
    $chosen=$_POST["sel_comp"] ;
    $query_even = mysqli_query($Conn, "Select comp.id, events.cid, events.event_name FROM events, comp WHERE events.cid = $chosen");

    while ($fetch = mysqli_fetch_assoc($query_even)){
        echo "<option value='{".$fetch['event_id']."}'>".$fetch['event_name']."</option>";
}
?>
</select>
    </div>
    <!-- Div to Show results -->
    <div id="results">
        <p>Resultados</p>
    </div>
    </form>
</div>
</body>
</html>

您的PHP代码在显示到用户浏览器之前会在服务器端进行解释。所以这段代码对用户将要做的选择一无所知

如果你想要一个"动态子选择",你需要添加一些javascript代码。该javascript代码将在用户的浏览器上运行,并能够对用户在第一个选择框上的操作做出反应。