How to set selected value of HTML select box with PHP
The manual way.....
<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'): ?> selected="selected"<?php endif; ?>>SEO и Блоговодство</option>
.....
The better way would be to loop through the interests
$interests = array(
'seo' => 'SEO и Блоговодство',
'auto' => 'Авто',
....
);
<select name="interest">
<?php foreach( $interests as $var => $interest ): ?>
<option value="<?php echo $var ?>"<?php if( $var == $result['interest'] ): ?> selected="selected"<?php endif; ?>><?php echo $interest ?></option>
<?php endforeach; ?>
</select>
<?php
$interests = array('seo' => 'SEO и Блоговодство', 'auto' => 'Aвто', 'business' => 'Бизнес', ...);
?>
<select name="interest">
<?php
foreach($interests as $k => $v) {
?>
<option value="<?php echo $k; ?>" <?php if($k == $result['interest']) ?> selected="selected" <?php } ?>><?php echo $v;?></option>
<?php
}
?>
</select>
<?php
$list='<select name="interest">
<option value="seo">SEO и Блоговодство</option>
<option value="auto">Авто</option>
<option value="business">Бизнес</option>
<option value="design">Дизайн</option>
...';
echo str_replace('value="' . $result['interest'] . '"','value="' . $result['interest'] . '" selected',$list);
?> This involves making a string containing your list, then using the string replace function to find the correct option and adding selected to the tag. If using XHTML you will need to use selected="selected".
http://sandbox.onlinephpfunctions.com/code/37eb8f5a213fe5a252cd4da6712f3db0c5558ae3