如何在选择框中显示[请选择],而不是在php中选择框上的第一项?

时间:2017-07-04 10:37:58

标签: php mysql

我已经使用php开发了选择框所有工作正常。但我想显示默认单词,如SELECT而不是selectbox中的第一项,我该怎么做。

                <?php
                    if(!isset($_POST['floor_id']))
                    {
                        $fl = 'floor_id';
                        $_POST['floor_id'] = $fl;
                    }

                    $query = "SELECT id,floor from sdh_extension.tb_floor";

                    $selectbox='<select name="floor_id" class="form-control" 
                    onchange="this.form.submit()">';
                    foreach ($db->query($query) as $row) 
                    {
                        $id=$row['id'];
                        $name=$row['floor'];

                        if($row['id'] == $_POST['floor_id'])
                        {

                             $isSelected = ' selected="selected"'; 
                        }
                        else {
                                $isSelected = ''; 
                                }
                        $selectbox.= "
                       <optionvalue=".$id.$isSelected.">".$name."</option>";
                    }
                        $selectbox.='</select>';
                        echo $selectbox;
                ?>

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助。

使用以下代码:

<?php
                if(!isset($_POST['floor_id']))
                {
                    $fl = 'floor_id';
                    $_POST['floor_id'] = $fl;
                }

                $query = "SELECT id,floor from sdh_extension.tb_floor";

                $selectbox='<select name="floor_id" class="form-control" 
                onchange="this.form.submit()">';
                $selectbox.= "
                   <option value="">Please select one option</option>";
                foreach ($db->query($query) as $row) 
                {
                    $id=$row['id'];
                    $name=$row['floor'];

                    if($row['id'] == $_POST['floor_id'])
                    {

                         $isSelected = ' selected="selected"'; 
                    }
                    else {
                            $isSelected = ''; 
                            }
                    $selectbox.= "
                   <option value=".$id.$isSelected.">".$name."</option>";
                }
                    $selectbox.='</select>';
                    echo $selectbox;
            ?>

答案 1 :(得分:0)

在select选项上使用禁用选项' SELECT ' 试试这个

<?php
if(!isset($_POST['floor_id']))
{
    $fl = 'floor_id';
    $_POST['floor_id'] = $fl;
}

$query = "SELECT id,floor from sdh_extension.tb_floor";

$selectbox='<select name="floor_id" class="form-control" 
onchange="this.form.submit()"> <option value=""  disabled>SELECT</option>"';
foreach ($db->query($query) as $row)
{
    $id=$row['id'];
    $name=$row['floor'];

    if($row['id'] == $_POST['floor_id'])
    {

        $isSelected = ' selected="selected"';
    }
    else {
        $isSelected = '';
    }
    $selectbox.= "
   <optionvalue=".$id.$isSelected.">".$name."</option>";
}
    $selectbox.='</select>';
    echo $selectbox;
?>