cookies - PHP $_COOKIE to remember checkbox result -
i'm trying project remember if user has pressed checkbox or not if return site.
on html form have following checkbox (will reload page onchange)
<input type="checkbox" name="showcabin" <?php echo $_cookie['selectedcabin']?> onchange="document.getelementbyid('maininput').submit()" > <label for="checkbox">show cabin</label>
the below php run when user first runs page
<?php print_r ($_cookie); //using see result stored on page load - blank(apart session id) //check see if check box selected prior page reload if (isset($_post['showcabin'])){ $selectedcabin = 'checked="checked"'; //if checkbox wasn't ticked when page loaded, there stored variable in cookie }elseif ($_cookie['selectedcabin'] == 'checked="checked"'){ $selectedcabin = 'checked="checked"'; }else{ //if not leave variable blank $selectedcabin =''; } $_cookie["selectedcabin"] = $selectedcabin; echo $_cookie["selectedcabin"]; ?>
i can't variable $selectedcabin retain information after close browser , reopen.
i'm aware there's ways javascript, javascript knowledge very limited, don't want go down route if possible
thanks!
you not setting cookie correctly. use this
<?php $cookie_name = "showcabin"; $cookie_value = "checked='checked'"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // cookie expire in 86400s = 1 day ?>
to set cookies
Comments
Post a Comment