如何在php中显示组合框中的选定值

时间:2013-10-25 12:12:01

标签: php html

我在PHP中选择了(组合框),在选择了一个之后,我用数据库中的数据填充第二个。 问题是如何在页面重新加载后从第一个组合框中显示我的选择。 我有以下代码:

    <select name="category" id="category" maxlength="30" onchange="this.form.submit();">
                                <option value =""></option>
                            <?php
                                require_once("config.php");
                                // Connect to server
                                $con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
                                //choose DB
                                mysql_select_db($DbName, $con);                             
                                $sql = "SELECT * FROM `category` ORDER BY `name`";
                                $res = mysql_query($sql);
                                while ($row1 = mysql_fetch_array($res)){
                                    echo '<option value ="'.$row1['name'].'">'.$row1['name'].'</option>';

                                }       
                            ?>
                            </select>
                        </span>
<label>race:</label>
<span>
                            <select name="race" id="race" maxlength="30" >
                                <option value =""></option>
                            <?php
                                if (isset($_POST['category'])) {
                                    $var = $_POST['category'];

                                    // Connect to server
                                    $con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
                                    //choose DB
                                    mysql_select_db($DbName, $con);     
                                    $sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";                                
                                    $res = mysql_query($sql);
                                    while ($row1 = mysql_fetch_array($res)){
                                        echo '<option value ="'.$row1['race'].'">'.$row1['race'].'</option>';
                                    }   
                                }
                            ?>
                            </select>

1 个答案:

答案 0 :(得分:3)

如何获取值的方法在属性form

method标记中定义

在您的情况下,您需要获取echo $_POST['category']并将其与选项的name进行比较

您的代码并不完美。看看如下::))

<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress, $ServerUser, $ServerPassword);
//choose DB
mysql_select_db($DbName, $con);

?>
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
    <option value=""></option>
    <?php
    $val = $_POST['category']?:'';

    $sql = "SELECT * FROM `category` ORDER BY `name`";
    $res = mysql_query($sql);
    while ($row1 = mysql_fetch_array($res)) {
        $selected = ($val == $row1['name'] ? 'selected="selected"' : '');
        echo '<option value ="' . $row1['name'] . '" '. $selected .'>' . $row1['name'] . '</option>';

    }
    ?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30">
    <option value=""></option>
    <?php
    if (isset($_POST['category'])) {
        $var = $_POST['category'];
        $sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
        $res = mysql_query($sql);
        while ($row1 = mysql_fetch_array($res)) {
            echo '<option value ="' . $row1['race'] . '">' . $row1['race'] . '</option>';
        }
    }
    ?>
</select>