akrisanov akrisanov - 1 year ago 68
PHP Question

HTML Select Box: set the option on selected with help PHP

I have html-code:

<select name="interest">
<option value="seo">SEO и Блоговодство</option>
<option value="auto">Авто</option>
<option value="business">Бизнес</option>
<option value="design">Дизайн</option>

In variable
saved value. How to set element option with

Answer Source

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; ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download