【问题标题】:How to set selected value of HTML select box with PHP如何使用 PHP 设置 HTML 选择框的选定值
【发布时间】:2011-04-14 09:59:49
【问题描述】:

我有下一块模板:

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

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

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

谢谢!

【问题讨论】:

    标签: php html


    【解决方案1】:

    手动方式.....

    <select name="interest">
        <option value="seo"<?php if($result['interest'] == 'seo'): ?> selected="selected"<?php endif; ?>>SEO и Блоговодство</option>
        .....
    

    更好的方法是遍历兴趣

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

    【讨论】:

    【解决方案2】:
    <?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>
    

    【讨论】:

      【解决方案3】:
      <?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);
      

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

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

      【讨论】:

        【解决方案4】:
        <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>
        

        【讨论】:

          【解决方案5】:
          <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>
          

          【讨论】:

            【解决方案6】:

            您需要让 PHP 为适当的 &lt;option&gt; 标签插入 selected="selected" 属性,如下所示:

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

            【讨论】:

            • 这似乎是一个很好的第一个答案,但我已经编辑了您的答案以提供更改的解释。 This 可能值得一读。
            • 当您回答问题时,不要只发布答案。提供有关您的解决方案的更多信息
            • 谢谢@AlastairBrown。对不起,这是我第一次来这里发布答案。下次我会记住这件事。
            【解决方案7】:

            另一种方式(在 WordPress 中使用)Link to Wordpress code 采用可重用性是:

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

            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' );
            }
            

            selected()函数依次调用下面的helper函数来比较判断该值是否被选中。它返回 '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;
            }
            

            【讨论】:

              【解决方案8】:

              简单的速记方法是

              //after pulling your content from database
              $value = interest['value'];
              <option value="seo" <?php echo ($value == "seo") ? 'selected = "selected"' : '' ;?> >SEO и Блоговодство</option>
              ...
              

              希望对你有帮助

              【讨论】:

                【解决方案9】:

                不需要写任何东西。 只需输入以下代码

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

                【讨论】:

                  猜你喜欢
                  • 2012-09-08
                  • 2012-05-12
                  • 1970-01-01
                  • 2013-09-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-10-04
                  相关资源
                  最近更新 更多