设置表单的背景

时间:2013-12-02 14:11:47

标签: php html5 cookies

当我选择一种颜色时,我只是在试图让我的表单改变颜色。对于这个即时通讯使用饼干..只有当我按下按钮时,背景并没有改变,但当你再次按它时它确实如此。为什么会这样呢?

这是我的代码(抱歉在php中使用html):

<?php

    $red = "";
    $blue = "";
    $green = "";
    $gold = "";
    $silver = "";
    $purple = "";

    $hour = time() + 3600;

    if (isset($_POST['order'])) {
        $color = $_POST['order'];
        $$color = "selected";
        setcookie("Free_cookies", $color, $hour);
    }
    else{
        $color = "red";
        $red = "selected";
    }
    if(isset($_COOKIE['Free_cookies'])){
        $color = $_COOKIE['Free_cookies'];
        $$color = "selected";
    }

?>
<form method='post'<?php echo "STYLE='background-color:".$color.";'";?> ><p id='txtorder'  >color: </p>
    <select name='order' id='order'>
        <option value="red"<?php echo $red; ?> >red</option>
        <option value="blue"<?php echo $blue; ?> >blue</option>
        <option value="green"<?php echo $green; ?> >green</option>
        <option value="gold"<?php echo $gold; ?> >gold</option>
        <option value="silver"<?php echo $silver; ?> >silver</option>
        <option value="purple"<?php echo $purple; ?> >purple</option>
    </select>
    <input type='submit' value='sort'/>
</form>

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试阅读仅使用setcookie()设置的Cookie。

在下一页加载之前,cookie无法读取(cookie本地存储在客户端而不是服务器上,因此PHP不会读取它,直到它被发送到客户端并再次返回)。

要修复,只有你的cookie读取如果没有$_POST值,即给新的POSTed值优先:

<?php

    $red = "";
    $blue = "";
    $green = "";
    $gold = "";
    $silver = "";
    $purple = "";

    $hour = time() + 3600;

    // first check for a new value, and use it as well as saving it for next time
    if (isset($_POST['order']))
    {
        $color = $_POST['order'];
        $$color = " selected";
        setcookie("Free_cookies", $color, $hour);
    }
    // if there's no new value, THEN check for a previous value in a cookie
    else if(isset($_COOKIE['Free_cookies']))
    {
        $color = $_COOKIE['Free_cookies'];
        $$color = " selected";
    } 
    // otherwise default to red
    else
    {
        $color = "red";
        $red = " selected";
    }

?>

<form method='post' <?php echo "STYLE='background-color:".$color.";'";?> ><p id='txtorder'  >color: </p>
    <select name='order' id='order'>
        <option value="red" <?php echo $red; ?> >red</option>
        <option value="blue" <?php echo $blue; ?> >blue</option>
        <option value="green" <?php echo $green; ?> >green</option>
        <option value="gold" <?php echo $gold; ?> >gold</option>
        <option value="silver" <?php echo $silver; ?> >silver</option>
        <option value="purple" <?php echo $purple; ?> >purple</option>
    </select>
    <input type='submit' value='sort'/>
</form>

答案 1 :(得分:1)

这是因为cookie记住了最后保存的颜色,并且您正在覆盖刚刚提交的颜色。只有在第二次提交后才会显示所需的颜色。你应该这样做:

if (isset($_POST['order'])) {
    $color = $_POST['order'];
    $$color = "selected";
    setcookie("Free_cookies", $color, $hour);
} else {
    if (isset($_COOKIE['Free_cookies'])) {
        $color = $_COOKIE['Free_cookies'];
        $$color = "selected";
    } else {
        $color = "red";
        $red = "selected";
    }
}

在这种情况下,它将始终采用您提交的发布颜色。如果你登陆页面并且有一个cookie,它就会接受。如果表单未提交且没有cookie,则默认使用红色。