简体   繁体   中英

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)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM