【问题标题】:Why does this PHP code set the initial default selected value as 'NONE'?为什么此 PHP 代码将初始默认选定值设置为“NONE”?
【发布时间】:2018-02-27 20:46:04
【问题描述】:

处理一个我没有开始的文件并遇到此代码的问题,我无法完全解决问题。

页面设置的 cookie 值等于在页面上的下拉选项中选择的秒数。然后,该选定值会以可能秒的间隔不断刷新页面。所有这些都可以正常工作,但是当第一次加载文件或没有设置秒值的 cookie 时,它​​默认为“无”。

我想要做的是让它默认为 15 秒值选择,而不是“无”。如果我在下面的数组中标出 None 值,它工作正常。但是,在某些极端情况下,有人可能必须将其设置为“无”。所以完全关闭这个能力是不可行的。

cookie 在此处创建:

function cookie_based_settings() {
        global $login_id;
        if (($_GET['set_refresh_time'] || !$_COOKIE["refresh_time_$login_tech_id"]) && !$norefreshcookie) {
                if (!$_COOKIE["refresh_time_$login_id"]) {
                        $_POST['refresh_time']='NONE';
                }
                setcookie("refresh_time_$login_id", $_POST['refresh_time'], time()+60*60*24*365, '/', 'website.domain.com');
                $_COOKIE["refresh_time_$login_id"]=$_POST['refresh_time'];
        }

}

cookie_based_settings();

这里是生成表单值的大部分代码,默认为“无”。谁能准确解释为什么选择始终选择“无”选项而不是其他值之一?我看不到。

<form method=POST action=the_file.php3?set_refresh_time=1 name=globe>Auto Refresh:<select name=refresh_time onchange='submit(globe);'><?
        # build array of refresh times (seconds):
        $refresh_times[]=15;
        $refresh_times[]=30;
        $refresh_times[]=45;
        $refresh_times[]=60;
        $refresh_times[]=120;
        $refresh_times[]=180;
        $refresh_times[]=240;
        $refresh_times[]=300;
        $refresh_times[]='NONE';
       foreach ($refresh_times as $key => $value) {
                if ($value==$_COOKIE["refresh_time_$login_id"]) {
                        $selected="selected";
                } else {
                        $selected='';
                }
                printf("<option %s value=%s>%s</option>",$selected,$value,$value);
        }
        ?></select></form>

HTML 输出如下所示:

<form method="POST" action="the_file.php3?set_refresh_time=1" name="globe">Auto Refresh:
 <select name="refresh_time" onchange="submit(globe);">
  <option value="15">15</option>
  <option value="30">30</option>
  <option value="45">45</option>
  <option value="60">60</option>
  <option value="120">120</option>
  <option value="180">180</option>
  <option value="240">240</option>
  <option value="300">300</option>
  <option selected="" value="NONE">NONE</option>
 </select>
</form>

我意识到选项标签的“选定”添加正在执行此操作,默认情况下,如果 cookie 不存在,它会选择它,但我不确定为什么。或者如何改变这种行为。理想情况下,我只想添加一个强制静态编码默认值的简单行:

$default_value=15;

【问题讨论】:

  • 它可能导致混乱和奇怪的副作用,使超级全局变量发生变异。
  • $login_tech_id$norefreshcookie 未定义。
  • 最好使用!isset 来测试您的cookie是否未定义。
  • 为什么在 cookie 名称中使用 $login_id?您是否设想许多用户使用相同的站点和浏览器具有不同的刷新率设置?如果您有许多用户设置要存储和检索,则数据库/存储可能更适合。你如何填充 $login_id?
  • @Progrock 使用 $login_id 让个人使用不同的刷新率。当前系统按编码工作,我只是想不通为什么表单值默认为“无”选项。如果我将该选项从数组中取出,则默认为“15”第二个选项。我只是没有看到通过此处选择“无”的逻辑的路径。在第一个代码部分有: $_POST['refresh_time']='NONE';我认为这是最初设置它的原因,但无论如何更改该行都没有影响。也许我确实需要阻止时间完全重写。

标签: php cookies refresh default option


【解决方案1】:

好的。好吧,我是个白痴。设置 $_POST['refresh_time']='NONE';到 '15' 的值。确实有效,正是我所需要的。我想用新的眼光再次看到它也有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2017-02-21
    • 2022-11-24
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多