如何使用 PHP 设置 HTML 选择框的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3747212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:57:54  来源:igfitidea点击:

How to set selected value of HTML select box with PHP

phphtml

提问by akrisanov

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'].

并将结果值存储在$result['interest'].

How can I mark optionelement as selected with PHP?

如何option使用 PHP将元素标记为已选择?

Thanks!

谢谢!

回答by Galen

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>

回答by Jacob Relkin

<?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>

回答by Jeffrey Black

<?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".

?> 这涉及制作一个包含您的列表的字符串,然后使用字符串替换功能找到正确的选项并将选定的选项添加到标签中。如果使用 XHTML,您将需要使用 selected="selected"。

http://sandbox.onlinephpfunctions.com/code/37eb8f5a213fe5a252cd4da6712f3db0c5558ae3

http://sandbox.onlinephpfunctions.com/code/37eb8f5a213fe5a252cd4da6712f3db0c5558ae3

回答by sshow

<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>

回答by Taha Farooqui

You need to get PHP to insert the selected="selected"attribute for the appropriate <option>tag, like this:

您需要让 PHPselected="selected"为适当的<option>标签插入属性,如下所示:

<?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>

回答by MuturiAlex

Another way ( as used in WordPress ) Link to Wordpress codewhich employs reusability would be:

另一种方式(在 WordPress 中使用)链接到使用可重用性的Wordpress 代码是:

    <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.

selected() 函数确定是否选择了该值并设置所选选项。

/**
 * 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 ''.

selected() 函数依次调用下面的辅助函数来比较和确定该值是否被选中。它返回 'selected=selected' 或 ''。

/**
 * 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;
}

回答by rahim asgari

<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>

回答by Marcel

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

我希望这有帮助