Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Checkbox problems

    My aim is to have a page where the user sets his preferences, and then submits the form where the preferences are saved to cookies. The form is submitted using the POST method.

    The preferences page works fine, however when saving the cookies I run into problems determining whether checkboxes were checked. They return on when checked, but don't seem to return anything when unchecked. However, using isset() doesn't seem to work properly. I've also tried setting a variable to the value of $_POST("checkbox") and then checking whether it's NULL, but that isn't working either.

    Any help would be appreciated, I have no idea what could be wrong or what other methods I could try.

    FYI: All irrelevant code was omitted from below, so that's why it may be a little strange.

    PHP Code:
    <?php

    if (isset($_GET["action"])) { $action $_GET["action"]; } else { $action "base"; }

    function 
    irc() {
      echo 
    "<div class='box'>";
      echo 
    "<form action='?action=save&save=irc' method='post'>";
      echo 
    "<table cellspacing='1' cellpadding='2'><tr>";
      echo 
    "<td title='Sets whether smileys should be replaced by graphical representations.'>";
      echo 
    "<label for='ircsmiley'>Display smileys</label></td>";
      echo 
    "<td><input type='checkbox' name='ircsmiley' id='ircsmiley' value='ircsmiley' checked='checked' /></td>";
      echo 
    "<td title='Sets whether timestamps should be displayed in chat.'>";
      echo 
    "<label for='irctimestamp'>Display timestamps</label></td>";
      echo 
    "<td><input type='checkbox' name='irctimestamp' id='irctimestamp' value='irctimestamp' checked='checked' /></td>";
      echo 
    "</tr><tr>";
      echo 
    "<td title='Enables the automatic channel rejoin when kicked out.'>";
      echo 
    "<label for='ircautocon'>Auto-rejoin</label></td>";
      echo 
    "<td><input type='checkbox' name='ircautocon' id='ircautocon' value='ircautocon' checked='checked' /></td>";
      echo 
    "<td title='If enabled, any received message containing the current user nick will be highlighted.'>";
      echo 
    "<label for='irchighnick'>Highlight nick</label></td>";
      echo 
    "<td><input type='checkbox' name='irchighnick' id='irchighnick' value='irchighnick' checked='checked' /></td>";
      echo 
    "</tr><tr>";
      echo 
    "<td colspan='4' title='Gives a list of words triggering highlight. Words are separated using spaces.'>";
      echo 
    "Highlight words: <input type='text' id='irchighwords' name='irchighwords' /></td>";
      echo 
    "</td><tr><td colspan='4'>";
      echo 
    "<input type='submit' value='Save Settings' />";
      echo 
    "<input type='button' value='Cancel' onclick='history.go(-1);' /></td>";
      echo 
    "</tr></table></form></div>";
    }

    function 
    save() {
      if (
    $_GET["save"] == irc) {
        if (isset(
    $_POST["ircsmiley"])) { setcookie("ircsmiley",true,time()+157680000); } else { setcookie("ircsmiley",false,time()+157680000); }
        if (isset(
    $_POST["irctimestamp"])) { setcookie("irctimestamp",true,time()+157680000); } else { setcookie("irctimestamp",false,time()+157680000); }
        if (isset(
    $_POST["ircautocon"])) { setcookie("ircautocon",true,time()+157680000); } else { setcookie("ircautocon",false,time()+157680000); }
        if (isset(
    $_POST["irchighnick"])) == "on") { setcookie("irchighnick",true,time()+157680000); } else { setcookie("irchighnick",false,time()+157680000); }
        
    setcookie("irchighwords",$_POST["irchighwords"],time()+157680000);
      }
    }

    function 
    showMessage() {
      echo 
    "<div class='box'>";
      echo 
    "<p>Settings saved successfully.</p>";
      echo 
    "<p><a href='settings.php'>Return to settings</a></p>";
      echo 
    "</div>";
    }

    if (
    $action == "save") { save(); }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="bluegrey.css" />
        <style type="text/css">
    //Styling
        </style>
      </head>

      <body>
    <?php

    if (isset($_GET["action"])) { $action $_GET["action"]; } else { $action "base"; }

    if (
    $action == "irc") { irc(); }
    if (
    $action == "save") { showMessage(); }

    ?>
      </body>
    </html>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    With the exception of the if (isset($_POST["irchighnick"])) == "on") phrase, your code worked fine for me. I tested it here. Instead of setting cookies I displayed "set" or "not set" and it all works fine.

    Can you be a little more specific as to what the problems are? You don't really go into that. FYI, isset() works just fine (Oh, and you are right about the checkbox behavior-- when it is not checked, it doesn't show up at all in $_POST)

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Strangely enough, it's working for me as well if I simply echo the result. Cookies seem to be a totally different problem, though. The cookies are simply not set if the checkbox is not checked. They work fine is the checkbox is checked, however. It only occurred to me after I posted that this is strange behaviour, because it means that it can't be a problem with isset() not working, because if so the behaviour of the cookies would simply be the same whether it is checked or unchecked.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    While you would think values of cookies can be boolean values, my guess is there's something lost in translation there. Try setting the cookie value to a 0 for false, 1 for true, or the strings 'on' and 'off', or a 'T' and 'F', and see if that works.

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    That works, thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •