基于下拉选择的操作

时间:2013-11-29 04:29:07

标签: php mysql forms

我想创建一个下拉列表,根据所选房间的类型显示可用房间列表,但下拉列表不显示任何值

<th align=left>ROOM TYPE :</th>
    <td>
    <?php
        echo "<select name=txttype>";

        $qup="select type from tariff where avroom > 0";
        $rs=mysql_query($qup);



        while($res=mysql_fetch_row($rs))
        {
            echo "<option value='".$res[0]."'>".$res[0]."</option>";
        }


        echo "<select>";
        echo "</td>";

            ?>


<tr>
    <?php
     $type=$_POST["txttype"];\\to be used for the if statement
     $name=$_POST["txtname"];
    ?>

    <th align=left>NO OF ROOMS   :</th>
    <td><select name=txtroom>
    <?php
    if($_POST["txttype"]=='standard'){
    for($i=1;$i<=avroom;$i++)\\ avroom is the field that contains the no of rooms
    {
        echo "<option value=$i>$i</option>";
    }
    }
    ?>

1 个答案:

答案 0 :(得分:0)

如果我的问题正确,可能是您正在寻找的解决方案

<?php
    $data   = array();
    $query  = "select type, avroom from tariff where avroom > 0";
    $result = mysql_query($query);

    while($row = mysql_fetch_row($result)) {
        $data[$row[0]] = $row[1];
    }
?>

<body>
    <table>
        <tr>
            <th align=left>ROOM TYPE :</th>
            <td><select id="txttype" name=txttype onchange="load_rooms(this)"></select></td>
        </tr>
        <tr>
            <th align=left>NO OF ROOMS :</th>
            <td><select id="txtroom" name=txtroom></select></td>
        </tr>
    </table>

    <script type="text/javascript">
        var data = <?php echo stripslashes(json_encode($data)); ?>;

        function populate_types() {
            var type = document.getElementById("txttype");
            for (var t in data) {
                var option   = document.createElement("option");
                option.value = option.text = t;
                type.add(option);
            }
            load_rooms(type);
        }

        function load_rooms(type) {
            var room = remove_childs(document.getElementById("txtroom"));
            for (var i = 1; i <= data[type.value]; i++) {
                var option   = document.createElement("option");
                option.value = option.text = i;
                room.add(option);
            }
        }

        function remove_childs(parent) {
            var fc = parent.firstChild;
            while(fc) {
                parent.removeChild(fc);
                fc = parent.firstChild;
            }
            return parent;
        }

        window.onload = populate_types;
    </script>
</body>

请注意,我假设avroom是您的数据库表字段,负责存储type字段中存储的特定房型的可用房间信息。