Schwesi
Schwesi

Reputation: 4904

How to set a placeholder for select drop down

I am trying to set a placeholder (like 'Choose...' or something similar) for my select drop down. I know how that would work in a 'static' drop down. However, I do not how that works in a database acquired drop down.

My code:

<label>Please choose an event:</label>
  <select id="select-event-type">
     <?php foreach ($this->events as $event) {
         echo "<option value='" .$event->event_id. "'>" .$event->event_title."</option>";
      }?>
</select>

Everything works fine. I just would like when the drop down loads that it says 'Choose...' instead of my first entry from my database and that the placeholder cannot be selected anymore after opening the drop down.
I am really new to this and I am very thankful for any kind of help!

Upvotes: 0

Views: 3087

Answers (2)

hashbrown
hashbrown

Reputation: 3516

<label>Please choose an event:</label>
  <select id="select-event-type">
     <?php 
      echo "<option selected>Choose...</option>";
      foreach ($this->events as $event) {
         echo "<option value='" .$event->event_id. "'>" .$event->event_title."</option>";
      }?>
</select>

Upvotes: 4

Sterling Archer
Sterling Archer

Reputation: 22405

Just put the option before your loop.

<label>Please choose an event:</label>

<select id="select-event-type">
    <option selected>Choose...</option>
    <?php foreach ($this->events as $event) {
        echo "<option value='" .$event->event_id. "'>" .$event->event_title."</option>";
    }?>
</select>

Upvotes: 7

Related Questions