PHP-创建相关的下拉列表

时间:2019-03-23 02:15:19

标签: php mysql

所以,我有两个下拉列表,其中填充了来自两个与外键连接的独立mysql表中的数据,我想知道我能否单独使用php使它们相互依赖(不使用ajax,jscript等)而不是在第二个表上使用一堆if语句?

到目前为止,我有:

   Movies:
   <select name="Movie">
   <option value="Pick a Movie">Pick a movie</option>
   <?php foreach($movies->fetchAll() as $movie):?>
   <option value="<?php echo $movie['id'];?>"> <?php echo 
  $movie['movietitle'];?> </option>
   <?php endforeach; ?>
 </select>

   Screening Time:
   <select name="times">
   <option value="Please select a Screening time">Please select a 
   screening time</option>
   <?php foreach($screenings->fetchAll() as $screening):?>
   <option value="<?php echo $screening['id'];?>"> <?php echo 
   $screening['time'];?> </option>
   <?php endforeach; ?>
 </select> 

我希望当用户从第一个下拉菜单中选择一部电影时,仅显示该特定电影的放映时间,而不是数据库中保存的所有放映时间。

我假设我需要一个外键来连接两个表,所以现在有两个表:

电影桌

|---------------------|------------------|
|           id        |    movietitle    |           
|---------------------|------------------|
|           1         |  pulp fiction    |           
|---------------------|------------------|
|           2         |    armageddon    |           
|---------------------|------------------|
|          3          |     Titanic      |           
|---------------------|------------------|

筛选表

|---------------------|---------------------|-------------|
|           id        |  screening time     | courseid(fk)|
|---------------------|---------------------|-------------|
|           1         |     sun 11          |       3     |
|---------------------|---------------------|-------------|
|           2         |      sat 8          |       1     |
|---------------------|---------------------|-------------|
|          3          |       fri 9         |       2     |
|---------------------|---------------------|-------------|

那么我能否调整我的代码以将Courseid包含为外键以使其依赖?如果没有其他方法,我可以强制用户只能在两个菜单中选择某些数据组,即可以选择 “ titanic”“星期五9”,但不是“ titanic”“ sun 11”或“ titanic”“ sat 8”?

1 个答案:

答案 0 :(得分:0)

要创建可靠的下拉菜单,您需要进行下拉菜单,每个下拉菜单均具有单独的 id 。使用更改事件,您可以连续访问服务器。然后,服务器将响应该请求。之后,您将处理请求并返回下拉菜单。 您可以将其放入while循环内。您可以在github website as reference

上查看完整的代码

遵循以下代码和流程

index.php

    <?php
if (file_exists('function.php')) {
    include_once 'function.php';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Dependable Dropdown Menu</title>
        <style>
            body{
                margin:0px auto;
                padding: 0px;
                max-width: 600px;
            }
            .main{

                margin: 50px auto;
                text-align: center;
            }
            #country,#city{
                width: 500px;
                height: 50px;
                font-size: 18px;
                margin: 0 auto;
            }
            select{
                width: 600px;
            }
        </style>

    </head>
    <body>
        <div class="main">
             <h1>Dependable Dropdown Menu</h1>
            <div class="dropdownmenu">
                <select name="" id="country" class="form-control">
                    <option value="">Select</option>
                    <?php
                    $con = connection();
                    $stmt = $con->query("select * from country order by name");
                    if ($stmt) {
                        ?>
                        <?php while ($r = $stmt->fetch_assoc()): ?>
                            <option value="<?php echo $r['id']; ?>"><?php echo $r['name']; ?></option>
                        <?php endwhile; ?>
                    <?php } ?>
                </select>
                <br/>
                <br/>
                <select name="" id="city" class="form-control">
                    <option value="">Select</option>

                </select>
            </div>
        </div>

        <script src="assets/js/jquery.min.js"></script>   
        <script>
            $('#country').on('change', function () { //dropdown changing event
                var countryID = $(this).val(); //getting specific country id from database
                console.log(countryID);
                if (countryID !== '') {
                    $.ajax({
                        url: "fetch.php", //requesting to fetch.php for getting cities list as response
                        data: {// we are passing two values by get in the url
                            action: 'getcities',
                            countryid: countryID
                        },
                        method: 'get',
                        success: function (data) { //getting response from server
                            $('#city').html(data); //putting response to second dropdown
                            console.log(data);
                        }, error: function (e) { //this will show error if exist any
                            console.log(e); //for looking error message press ctrl+shift+i
                        }
                    });
                } else {
                    $('#city').html("<option>Select</option>");
                }
            });
        </script>

    </body>
</html>

fetch.php

    <?php
if (file_exists('function.php')) {
    include_once 'function.php';
}
$con = connection();
if (isset($_GET['action']) && $_GET['action'] == 'getcities') {
    $countryid = mysqli_real_escape_string($con, $_GET['countryid']);
    $q = "select * from city where country='$countryid' order by name asc";
    $stmt = $con->query($q);
    $val = "";
    if ($stmt) {
        if ($stmt->num_rows > 0) {
            while ($row = $stmt->fetch_assoc()) {
                $val .= "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
            }
        }
    } else {
        $val = "<option>Select</select>";
    }
    echo $val;
}

function.php

    <?php
//connection for project. this connection will be used in whole project
function connection() {
    $con = new mysqli('localhost', 'root', '', 'country_city');
    return $con;
}