Set OPTION as Selected based on Bank values

Asked

Viewed 125 times

0

I need to create a condition so that if the bank option is Smartphone or Common Device that appears in Selected, otherwise it appears as "Select".

<div class="form-group col-md-3">
    <label>Aparelho</label>
    <select name="aparelho" class="form-control">                                                                                        
        <option selected> <?php echo $row_tel['aparelho'];?> </option>                                                                                       
        <option>Smartphone</option>
        <option>Aparelho Comum</option>
    </select>
</div>

Currently, it’s getting this way:

inserir a descrição da imagem aqui

Because what is currently registered the bank is the "Common Device" then appears the value registered in the bank and the options of "Selected".

1 answer

0

you can do a test on each option and display Selected if value is the same as bank value.

<div class="form-group col-md-3">
<label>Aparelho</label>
    <select name="aparelho" class="form-control">                                                                                        
        <option <?php if($row_tel['aparelho'] == 'Smartphone') {echo 'selected'; } ?> value="Smartphone">Smartphone</option>
        <option <?php if($row_tel['aparelho'] == 'Aparelho comum') {echo 'selected'; } ?> value="Aparelho comum">Aparelho comum</option>
    </select>
</div>

A tip, preferably by saving an id of each device type in the database:

<div class="form-group col-md-3">
<label>Aparelho</label>
    <select name="aparelho" class="form-control">                                                                                        
        <option <?php if($row_tel['aparelho'] == 1) {echo 'selected'; } ?> value="1">Smartphone</option>
        <option <?php if($row_tel['aparelho'] == 2) {echo 'selected'; } ?> value="2">Aparelho comum</option>
    </select>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.