0

Basically I am trying to check what's currently on my database and put it as default option with help of some PHP. Could you show me a solution how to adjust my code or any alternative for this?

I tried adding select="selected" by PHP

<select name="offers" required>
<option value="yes" <?php if($offers=="yes"){?>selected="selected"<?php}?> >yes</option>
<option value="no" <?php if($offers=="no"){?> selected="selected"<?php}?> >no</option>
</select>

I just want to make it automatically choose option as selected which is equal to the variable that I get from the database

Human Code
  • 93
  • 9
  • Are you creating the checkboxes dynamically ? – Houssem Apr 17 '19 at 21:08
  • Possible duplicate of [selected value get from db into dropdown select box option using php mysql error](https://stackoverflow.com/questions/18733545/selected-value-get-from-db-into-dropdown-select-box-option-using-php-mysql-error) – snwflk Apr 17 '19 at 22:54

1 Answers1

0

You don’t need selected=‘selected’, just have selected as it runs off a Boolean; I.e. selected=true (the option will be selected) or selected=false (default - the option will not be selected). For example;

<select name="offers" required>
    <option value="yes" <?php echo $offer=="yes" ? 'selected' : ''; ?> >yes</option>
    <option value="no" <?php echo $offer=="no" ? 'selected' : ''; ?> >no</option>
</select>
James Gedny
  • 112
  • 1
  • 8