php下拉选择值

时间:2017-06-16 12:43:38

标签: php dropdown

我正在编辑表单并在带有电影标题的下拉框中。 其中一个应该被选中,所以说DVD_id:100。 如何在下面的代码中完成这项工作?

        <div class="form-group">
        <label for="Site name" class="col-md-4 control-label">DVD Title</label>
        <?php
        $query = "Select DISTINCT dvd.dvdname, dvd.dvd_id, FROM DVD where ORDER BY dvd.dvdname";
        $result = mysql_query($query) or die ("Couldn�t execute query.");?>
        <div class="col-md-8">
            <select id="box" name="dvdtitlefield" class="selectpicker form-control" multiple title="Choose one of the following..." >
                <?php
                while ($row = mysql_fetch_assoc($result))
                {
                    extract($row);
                    echo '<option value="'. $dvd_id .'">'. $dvdname .'</option>';
                }?>
            </select>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

使用option标签的selected属性默认选择

<select>
<option value="tiger">tiger</option>
<option value="lion">lion</option>
<option value="zebra">zebra</option>
<option value="cat" selected>cat</option>
</select>

在您的代码中,您可以检查默认情况下要选择的选项并为其应用if条件

例如

<div class="form-group">
    <label for="Site name" class="col-md-4 control-label">DVD Title</label>
    <?php
    $query = "Select DISTINCT dvd.dvdname, dvd.dvd_id, FROM DVD where ORDER BY dvd.dvdname";
    $result = mysql_query($query) or die ("Couldn�t execute query.");?>
    <div class="col-md-8">
        <select id="box" name="dvdtitlefield" class="selectpicker form-control" multiple title="Choose one of the following..." >
            <?php
            while ($row = mysql_fetch_assoc($result))
            {
                extract($row);
                if($dvd_id==100)
                {
                echo '<option value="'. $dvd_id .'" selected>'. $dvdname .'</option>';
                }
                else
                {
                echo '<option value="'. $dvd_id .'">'. $dvdname .'</option>';
                }
            }?>
        </select>
    </div>
</div>