How to set selected value of HTML select box with PHP
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!
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
<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'){ echo ' selected="selected"'; } ?>>SEO</option>
<option value="auto"<?php if($result['interest'] == 'auto'){ echo ' selected="selected"'; } ?>>Auto</option>
<option value="business"<?php if($result['interest'] == 'business'){ echo ' selected="selected"'; } ?>>Business</option>
<option value="design"<?php if($result['interest'] == 'design'){ echo ' selected="selected"'; } ?>>Design</option>
</select>
<select name="interest">
<option value="seo" <?php echo $result['interest'] == 'seo' ? 'selected' : ''?> >SEO и Блоговодство</option>
<option value="auto" <?php echo $result['interest'] == 'auto' ? 'selected' : ''?>>Авто</option>
<option value="business" <?php echo $result['interest'] == 'business' ? 'selected' : ''?>>Бизнес</option>
<option value="design" <?php echo $result['interest'] == 'design' ? 'selected' : ''?>>Дизайн</option>
You need to get PHP to insert the selected="selected"
attribute for the appropriate <option>
tag, like this:
<?php $result=$userRow['ad_type']; ?>
<select class="form-control" name="ad_type">
<option <?php if($result == 'text'){ echo ' selected="selected"'; } ?> value="text">Text</option>
<option <?php if($result == 'image'){ echo ' selected="selected"'; } ?> value="image">Image</option>
<option <?php if($result == 'video'){ echo ' selected="selected"'; } ?> value="video">Video</option>
<option <?php if($result == 'htmladsense'){ echo ' selected="selected"'; } ?> value="htmladsense">Html Adsense</option>
</select>
Another way ( as used in WordPress ) Link to Wordpress code which employs reusability would be:
<select name='result[interest]' style="width:400px">
<option value='SEO' <?php selected($result['interest'], 'SEO'); ?>>SEO</option>
<option value='AUTO' <?php selected($result['interest'], 'AUTO'); ?>>Auto</option>
</select>
The selected() function determines if the value was choosen and sets the selected option.
/**
* Outputs the html selected attribute.
*
* Compares the first two arguments and if identical marks as selected
*
* @since 1.0.0
*
* @param mixed $selected One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function selected( $selected, $current = true, $echo = true ) {
return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
The selected() function in turn calls the helper function below to compare and determine whether the value is the selected or not. It returns 'selected=selected' or ''.
/**
* Private helper function for checked, selected, disabled and readonly.
*
* Compares the first two arguments and if identical marks as $type
*
* @since 2.8.0
* @access private
*
* @param mixed $helper One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @param string $type The type of checked|selected|disabled|readonly we are doing
* @return string html attribute or empty string
*/
function __checked_selected_helper( $helper, $current, $echo, $type ) {
if ( (string) $helper === (string) $current ) {
$result = " $type='$type'";
} else {
$result = '';
}
if ( $echo ) {
echo $result;
}
return $result;
}
Keep your scripting DRY and clean by setting up an array to contain your option data. Loop through the array and print the data into a template string with placeholders to avoid ugly, clumsy interpolation, concatenation, and inline conditions.
Code: (Demo)
$interests = [
'seo' => 'SEO и Блоговодство',
'auto' => 'Aвто',
'business' => 'Бизнес',
'design' => 'Дизайн',
];
$selected = 'business';
echo '<select name="interest">';
foreach ($interests as $value => $text) {
printf(
'<option value="%s"%s>%s</option>',
$value,
$selected === $value ? ' selected' : '',
$text
);
}
echo '</select>';
For anyone who puts in the extra effort to produce line-separated options with appropriate tabbing, there are a few ways to add tabs and newlines while printing in PHP, but I'll not demonstrate them here.
Simple short hand method would be
//after pulling your content from database
$value = interest['value'];
<option value="seo" <?php echo ($value == "seo") ? 'selected = "selected"' : '' ;?> >SEO и Блоговодство</option>
...
I hope this helps
Don't need to write anything. Just type the below code
`<div class="form-group">
<label for="title">Blog Trending Or Not ?</label><br>
<select class="custom-select" id="inputGroupSelect01" name="interest">
<option value="seo"<?php if($row['is_trending']=="seo"){echo "selected";} ?>>SEO и Блоговодство</option>
<option value="auto"<?php if($row['is_trending']=="auto"){echo "selected";} ?>>Авто</option>
<option value="business"<?php if($row['is_trending']=="business"){echo "selected";} ?>>Бизнес</option>
<option value="design"<?php if($row['is_trending']=="design"){echo "selected";} ?>>Дизайн</option>
</select>
</div>`
精彩评论