View Full Version : Checkbox problems

name _F1
01-27-2007, 07:43 AM
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.


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

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"

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


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

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


01-27-2007, 04:25 PM
With the exception of the if (isset($_POST["irchighnick"])) == "on") phrase, your code worked fine for me. I tested it here (http://www.onlinefamilyconnection.com/ofctest/crap.php?action=irc). 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)

name _F1
01-28-2007, 03:18 AM
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.

01-28-2007, 04:17 AM
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.

name _F1
01-28-2007, 08:30 AM
That works, thanks for your help. :thumbsup: