thomas walker
thomas walker

Reputation: 39

How to insert an empty option on a dropdown menu where information is pulled from a database?

This is the code I've got so far:

        <label for="course">Course</label>
        <select name="course" class="form-control" style="margin-bottom:2%;">
        <?php 

        $sql="SELECT course_name FROM course";
        $result = mysqli_query($conn, $sql) or die(mysql_error());


        while ($row = mysqli_fetch_array($result)) {
            echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
                }
        ?>
        </select>

I'm trying to make it so the dropdown appears blank at first instead of showing an option pulled from the database. (all connection etc is above)

Upvotes: 0

Views: 159

Answers (1)

vietanhyt
vietanhyt

Reputation: 668

Alex answered this question for you. Just add <option>Select...</option> before the loop (Change Select... to white space if you really want it to show a "blank" option).

Here is the code:

<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
    <option>Select...</option>
    <?php
    $sql="SELECT course_name FROM course";
    $result = mysqli_query($conn, $sql) or die(mysql_error());
    while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
    }
    ?>
</select>

Upvotes: 1

Related Questions