我正在尝试让我的项目记住用户是否已按下复选框,他们是否返回我的网站。
在HTML表格上,我有以下复选框(将重新加载页面Onchange)
<input type="checkbox" name="showcabin" <?php echo $_COOKIE['selectedcabin']?> onchange="document.getElementById('maininput').submit()" >
<label for="checkbox">Show Cabin</label>
当用户首先运行页面
时,以下PHP运行<?php
print_r ($_COOKIE); //using this to see what result is stored on page load - always blank(apart from session ID)
//check to see if check box was selected prior to page reload
if (isset($_POST['showcabin'])){
$selectedcabin = 'checked="checked"';
//If checkbox wasn't ticked when page loaded, is there a stored variable in a cookie
}elseif ($_COOKIE['selectedcabin'] == 'checked="checked"'){
$selectedcabin = 'checked="checked"';
}else{
//if not then leave variable blank
$selectedcabin ='';
}
$_COOKIE["selectedcabin"] = $selectedcabin;
echo $_COOKIE["selectedcabin"];
?>
我只是在关闭浏览器并重新打开浏览器后无法获得变量$ selectedcabin来保留信息。
我知道有可以使用JavaScript执行此操作的方法,但是我的JavaScript知识非常有限,所以如果可能的话,我不想走这条路线
谢谢!
您无法正确设置cookie。使用类似的东西
<?php
$cookie_name = "showcabin";
$cookie_value = "checked='checked'";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // The cookie will expire in 86400s = 1 day
?>
设置cookie