开发者

Checkbox checked value passed through several php pages

So I am trying to create a checkbox that I can pass it's value or have it's value stored in a cookie, so I can use it across several different pages.

Currently I have this:

<input type="checkbox" id="customLogoCheckbox" name="customLogo" onClick="window.open('vtest.php', '_blank','width=300,height=150,left=25,top=25,scrollbars=yes')" value="Show Active">Use Custom Logo

    <?
    if (customLogoCheckbox.ch开发者_如何学Pythonecked == true)
    {
        $_SESSION['logoCheck'] = 1;
    }
    else
    {
        $_SESSION['logoCheck'] = 0;
    }

And it just sets the cookie to 1 and doesn't allow it to change back to zero when I uncheck the checkbox, any ideas?


This should work:

if( isset($_REQUEST['customLogo']) && $_REQUEST['customLogo'] == "Show Active" )
   $_SESSION['logoCheck'] = 1;
else
   $_SESSION['logoCheck'] = 0;

First check if customLogo checkbox is set, then check it's value, and if everything is fine then set logoCheck to 1. If customLogo value is not set or it's not "Show Active" then the variable is set to 0 and your choice is reverted.
This is an example of how it works:

<?php
//start session
session_start();
//check if form as been submitted
if( isset( $_REQUEST['submit'] ) {
   if( isset($_REQUEST['customLogo']) && $_REQUEST['customLogo'] == "Show Active" )
      $_SESSION['logoCheck'] = 1;
   else
      $_SESSION['logoCheck'] = 0;
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
    <input <? if( $_SESSION['logoCheck'] == 1 ) echo "checked=\"checked\"" ?> type="checkbox" id="customLogoCheckbox" name="customLogo" value="Show Active"> Use Custom Logo<br />
    <input type="submit" name="submit" value="Ok"/>
</form>


You should apply a condition upon the $_POST or $_GET variable, not on some php-unknown customLogoCheckbox.checked kind of variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜