pal
pal

Reputation: 105

Set the default value in a drop down menu where data is entered dynamically

I have a drop down menu which is populated dynamically. Im trying to set one of the options as the default value. Given that the options are populated in the drop down only at run time, I cannot use

<option selected=selected> No </option> 

My code is as follows:

<div name="form.option" class="aob-div-dyn-field">
<tbody>
    <tr>
        <td class="label-section" >
            <label class="main-label">Choose your options</label>
        </td>
        <td class="input-section-dyn">
            <select class="main-input-select" ></select>
                 </td>
    </tr>
       </tbody>
      </div>

This refers to another file which has the enum declared.

enum options {
YES:"YES"
NO: "NO"
}

This line refers the enum "options" to the field option.

attribute optional option: options "option"

Any ideas on how I can proceed with this?

Upvotes: 0

Views: 93

Answers (1)

technology101010
technology101010

Reputation: 774

Yes you can do what you need just declare the first selected option like you have and then loop through all remaining items below. Just keep it outside of the loop. If "NO" is in your loop then just check for that and continue so you don't have a double up of options. Example:

<?php $a = results_array(); ?>

<option selected=selected> No </option>
<? foreach($results as $key => $value): if( $value == 'NO' ){ continue; } ?>
    <option value=<?php echo $key; ?>><?php echo $value; ?></option>
<?php endforeach; ?> 

Upvotes: 1

Related Questions