Display stored value form input when using options

I have a form that is working well until I decided to limit user input to ‘Yes’ or ‘No’. If the option is chosen it stores correctly to the database. The problem is that when I load the form (it’s a form for updating) the inputs that use the options only display the first option - in this case ‘Yes’. Here are two form inputs:

    <div class="form-group">
      <label for="givenname">Given Name</label>
      <input value="<?= $volunteer->givenname; ?>" type="text" name="givenname" maxlength= "16" id="givenname" class="form-control">
    </div>
    
    <div class="form-group">
      <label for="active">Active (Yes No)</label>
      <select input value="<?= $volunteer->active; ?>" name="active" id="active" class="form-control"> 
        <option value="Yes">Yes</option>
        <option value="No">No</option>
      </select>
    </div>

The first one correctly displays the correct ‘givenname’ stored in the database. The second displays ‘Yes’ every time. How can I display ‘Yes’ and ‘No’ correctly?

Thanks

You would output a selected attribute inside the <option ...> tag for the existing choice. So that you are not repeating code for every choice, it is simpler to have an array of the choices, then loop over the array to dynamically build the options.

Note: a <select ...> tag does not have an input or a value attribute.

Lastly, if you put the closing </label> tag after the field it corresponds to, you can leave out the for=’…’ attribute and the matching id=’…’ attribute, simplifying your code.

Thanks for that. After a little testing I got it to work. I also slimmed down my code per your suggestion. Tx

Sponsor our Newsletter | Privacy Policy | Terms of Service