php - Not able to get saved value from db mysql as selected value in dropdown. I trying to update the form which user has already filled -


i trying data database values reflecting saved value dropdown not coming selected.

<div class="col-md-4">                         <select name="officename" id="officename" class="form-control"  onchange="gettext(this)" required>             <?php              while($data = dbfetchassoc($result)){             ?>              <option value=''><?php echo $data['off_name']; ?></option>             <?php              }//while             ?>             </select>     </div> 

this common issue.

let's have select statement populated key value pairs database:

<select name="sel"> <?php foreach ($options $value => $label) : ?> <option value="<?= $value ?>"><?= htmlentities($label) ?></option> <?php endforeach ?> 

when form submitted, you'll value of sel in $_get or $_post (or $_request).

<?php $selected = $_post['sel']; ?> 

to select corresponding option, update above code so:

<select name="sel"> <?php foreach ($options $value => $label) : ?> <option <?php if ($selected == $value) : ?>selected<?php endif ?> value="<?= $value ?>"> <?= htmlentities($label) ?> </option> <?php endforeach ?> 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -