name _F1
01-27-2007, 06: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.
<?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>
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
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>