dpqg86714 2014-06-17 09:24
浏览 15
已采纳

保留HTML表单上从数据库收到的数据

<?php
include_once 'database_connect.php';
$conn = new dbconnection();
$dbcon = $conn->connect();
if (!$dbcon) {
    die("Fail".mysqli_error($dbcon));
}
?>
<html>
<head>
    <title></title>
    <script type="text/javascript"></script>
</head>
<body>
<form name="frm" method="post" action='<?php echo $_SERVER['PHP_SELF']; ?>'>
    <table width="50%" border="1" cellpadding="3" cellspacing="3" align="center">
        <?php
        $value1 = array();
        $select_query = "SELECT Distinct branch FROM subjects";
        $result = mysqli_query($dbcon, $select_query);
        if (!$result) {
            die("Fail".mysqli_error($dbcon));
        }

        while ($row = mysqli_fetch_array($result)) {
            $value1[] = $row['branch'];
        }

        ?>
        <tr>
            <td>Branch
            <td><select name="branch" id="branch" onchange="document.frm.submit();">
                    <option>Select Branch</option>
                    <?php
                    foreach ($value1 as $gets)
                        echo "<option value={$gets}>{$gets}</option>";
                    ?>
                </select>

                <?php
                $value2 = array();
                if (isset($_POST['branch'])) {
                    $branch = $_POST['branch'];
                    $getsub_query = "SELECT sub_code FROM subjects where branch='$branch'";
                    $result2      = mysqli_query($dbcon, $getsub_query);
                    if (!$result2) {
                        die("Fail
".mysqli_error($dbcon));
                    }
                    while ($row1 = mysqli_fetch_array($result2)) {
                        $value2[] = $row1['sub_code'];
                    }
                }
                ?>
        <tr>
            <td>Subject Code
            <td><select name="subcode" id="subcode">
                    <option>Subject Code</option>
                    <?php
                    foreach ($value2 as $gets)
                        echo "<option value={$gets}>{$gets}</option>";
                    ?>
                </select>

This code gets the first drop down list branch from data base. When we select value from it, second drop down list get filled from database . but problem is when i select option in first drop down list, the selected option does not remain their in first drop down list . but second drop down list fills correctly. i want that option i have selected should remain selected. Like its state should be changed. I think first drop down list gets filled again on form load.

  • 写回答

1条回答 默认 最新

  • douzengjian1535 2014-06-17 09:40
    关注

    You have to mention in your html that the option is indeed selected.

    Try replacing

    echo "<option value={$gets}>{$gets}</option>";
    

    By

    $selected = '';
    if (isset($_POST['branch'] && $gets==$_POST['branch']) {
      $selected = ' selected="selected"';
    }
    echo "<option value={$gets}".$selected.">{$gets}</option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么