I have a next piece of the template:
<select name="interest">
    <option value="seo">SEO и Блоговодство</option>
    <option value="auto">Авто</option>
    <option value="business">Бизнес</option>
    <option value="design">Дизайн</option>
    ...
and store resulting value in $result['interest'].
How can I mark option element as selected with PHP?
Thanks!
PHP 8 Get Single Selected Values of Select Box php if(isset($_POST['submit'])){ if(! empty($_POST['Fruit'])) { $selected = $_POST['Fruit']; echo 'You have chosen: ' . $selected; } else { echo 'Please select the value. '; } } ?>
To get value of a selected option from select tag:php if(isset($_POST['submit'])){ $selected_val = $_POST['Color']; // Storing Selected Value In Variable echo "You have selected :" .
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
it needs to go into a script tag (i.e. <script>... </script> ) so in PHP you could add that to an echo statement - e.g. echo "<script type=\"text/javascript">var...
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>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With