Setting a select's selected value


/ Published in: PHP
Save to your folder(s)

dynamically selects a value in a select according to what's already in the DB


Copy this code and paste it in your HTML
  1. function selecciona($dis)
  2. {
  3. $arr = array('None', 'option 1', 'option 2', 'option 3', 'option 4');
  4. for($i = 0; $i < count($arr); $i++)
  5. {
  6. $selected = ($arr[$i] == $dis) ? 'selected="selected"' : '';
  7. echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>\n";
  8. }
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.