Key K
Key K

Reputation: 169

How to make placeholder for 'select' box which fetch's data from mySQL?

I'm trying to add placeholder for my drop-box which gets results from mySQL and I don't know is there a way or not.

<div class="select_drp">
    <?php
        $query = mysql_query("SELECT * FROM training_list"); 
        echo '<select class="select_drp" name="insert_training" required>'; 
        while ($row = mysql_fetch_array($query)) {
           echo '<option value="'.$row['id'].'">'.$row['training_type'].'</option>';
        }
        echo '</select>';
        echo "<br/><br/>";
    ?>
</div>

Upvotes: 3

Views: 1879

Answers (3)

Razib Al Mamun
Razib Al Mamun

Reputation: 2711

Try this :

<div class="select_drp">
    <?php
        $query = mysql_query("SELECT * FROM training_list"); 
        echo '<select class="select_drp" name="insert_training" required>'; 
        echo '<option value="" disabled selected style="display: none;">Please Choose</option>'; //This line your placeholder
        while ($row = mysql_fetch_array($query)) {
           echo '<option value="'.$row['id'].'">'.$row['training_type'].'</option>';
        }
        echo '</select>';
        echo "<br/><br/>";
    ?>
</div>

Upvotes: 4

S.I.
S.I.

Reputation: 3375

If you talk for option inside the results from MySQL you can disable it like that

echo '<option value="'.$row['id'].'" disabled>'.$row['training_type'].'</option>';

If you talk for placeholder in select dropdown along with results from MySQL before your loop simply put another

<select>
    <option value="">placeholder</option>
    <?php
       ...
       while ( ... ) {
            echo '<option value="'.$row['id'].'">'.$row['training_type'].'</option>';
       }
     ?>
 </select>

Upvotes: 2

Gayan
Gayan

Reputation: 2935

Check this, I've added <option value="">Placeholder</option>, I think you can use this as a placeholder.

<div class="select_drp">
    <?php
    $query = mysql_query("SELECT * FROM training_list");
    echo '<select class="select_drp" name="insert_training" required>';
    echo '<option value="">Placeholder</option>';
    while ($row = mysql_fetch_array($query)) {
        echo '<option value="' . $row['id'] . '">' . $row['training_type'] . '</option>';
    }
    echo '</select>';
    echo "<br/><br/>";
    ?>
</div>

Upvotes: 4

Related Questions