dongpang1898 2019-06-17 10:56
浏览 17

PHP和HTML中的依赖下拉列表

I tried to implement a dependent drop down lists system. The code in contact.php page is:

<?php 
    $link = new mysqli("localhost", "root", "", "graphicdesign");
    if($link->connect_error){
        die("ERROR: Nu s-a putut realiza conexiunea la baza de date " .$link->connect_error);
    }

    $resultSet = $link->query("SELECT * FROM orase") or die('Error In Session');
    /* $rowsn = mysqli_fetch_array($resultSet);
    $n_oras=$rowsn['denumire_oras'];
    $id_orasss=$rowsn['id_oras'];    */                                         

    //$resultSetRep = $link->query("SELECT id_oras, denumire_rep FROM reprezentante where id_oras='$id_oras'") or die('Error In Session');                                                  
    //$rows1= mysqli_fetch_array($resultSetRep);
?>

<!DOCTYPE HTML>
<html>

<head>
    --head info
</head>

<body>
    <form action="#">

        <div class="row form-group">
            <div class="col-md-12">
                <label style="margin-right:15px;">Oras</label>
                <select id="denum_oras" name="den_oras">
                    <?php
                        while($rows = mysqli_fetch_array($resultSet)) {
                            $n_oras=$rows['denumire_oras'];
                            $id_oras=$rows['id_oras'];                                              
                            echo "<option value='$id_oras'>$n_oras</option>";
                        }
                    ?>
                </select>
            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-12">
                <label style="margin-right:15px;">Reprezentanta</label>
                <select id="reprez" name="reprez">
                    <?php
                        while($rows2=$resultSet->fetch_assoc()) {
                            $id_oras=$rows2['id_oras'];
                            $den_rep=$rows2['denumire_rep'];

                            $resultSetRep = $link->query("SELECT id_oras, denumire_rep FROM reprezentante where id_oras='$id_oras'") or die('Error In Session');

                            while($rows3=$resultSetRep->fetch_assoc()) {
                                $id_rep = $rows3['id_rep'];
                                $den_rep = $rows3 ['denumire_rep'];

                                echo "<option value='$id_rep'>$den_rep</option>";
                            }
                        } 
                    ?>
                </select>
            </div>
        </div>

    </form>
</body>

</html>

The first drop down list is working, it is retrieving the right data from table "orase" in the database: enter image description here

But for the second drop down, i want that, when i select the option "Braila" form the first drop down, to show the values from the database with the foreign key "id_oras" as the selected choice.

In this case, when I select "Braila" from the first drop down list, with the id_oras=1 in the table orase, I want that the second drop down list to retrieve data from the table "reprezentante" where id_oras = 1, in this case to retrieve values "Rep Braila" and "Rep Braila 2" to be shown in the drop down, but this is not happening..

enter image description here

This is a capture of the page: enter image description here

The code i posted is the best one i thought about, but still doesn't work.. please help me!

Thanks!

  • 写回答

3条回答 默认 最新

  • dongping5230 2019-06-17 11:08
    关注

    PHP is preprocessor language isn't compile on runtime you can use Ajax or XMLHttpRequest to get the data. After that, you can use Jquery or javascript to bind the data in the select box

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line