MySQLi选择列表取决于之前选择的

时间:2015-06-30 16:15:13

标签: php mysqli

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

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

Connection.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);

?>

Results.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>

1 个答案:

答案 0 :(得分:0)

在向用户的浏览器显示之前,您的PHP代码在服务器端进行解释。因此,此代码无法了解用户将做出的选择。

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