I have this page that sets a cookie and echos out a string if you check a checkbox. The string prints correctly, but the cookie never gets set and I have no idea why.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="checkbox">Option 1:</label>
<input type="checkbox" name="checkbox" id="checkbox"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['checkbox'])) {
setcookie("cookie", "on", time()+3600*24);
echo "You checked the checkbox and a cookie was set with a value of:<br>";
}
else {
setcookie("cookie", "off", time()+3600*24);
echo "You didn't check the checkbox and a cookie was set with a value of:<br>";
}
echo $_COOKIE['cookie'];
?>
Does anyone know why the above code does not work?