-2

i have developed select box using php all work fine. but i want to display default word like SELECT instead of first item in selectbox, how can i do it.

                <?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;
                ?>
Hasee
  • 7
  • 1
  • 5
  • 2
    Possible duplicate of [How do I make a placeholder for a 'select' box?](https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box) – Lars Beck Jul 04 '17 at 10:40

2 Answers2

0

Hope this will help.

Use the below code to:

<?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;
            ?>
mageDev0688
  • 566
  • 4
  • 27
0

Use a disable option 'SELECT' on the select option Try this

<?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;
?>
Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51