etrader Posted September 8, 2011 Share Posted September 8, 2011 I want to update a value by setting cookie. I use this code: <?php if (isset($_POST['ChangeOrdering'])) { setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000); } echo $_COOKIE["Ordering"]; ?> <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>"> Reorder messages: <select name="ChangeOrdering"> <option value="DateAdded ASC">Oldest first</option> <option value="DateAdded DESC">Newest first</option> <option value="Title ASC">By Title, A-Z</option> <option value="Title DESC">By Title, Z-A</option> </select> <input type="submit" value=" Save Settings " /> </form> Problem is that echo will return the previous cookie set, not the last value. As a matter of fact, my problem is about updating the cookie. It seems that the browser load the cookie before it is updated by the POST value. Quote Link to comment https://forums.phpfreaks.com/topic/246680-set-and-update-cookie-by-a-select-form/ Share on other sites More sharing options...
Pikachu2000 Posted September 8, 2011 Share Posted September 8, 2011 The value of a cookie is not available until the next page load. Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Quote Link to comment https://forums.phpfreaks.com/topic/246680-set-and-update-cookie-by-a-select-form/#findComment-1266699 Share on other sites More sharing options...
etrader Posted September 8, 2011 Author Share Posted September 8, 2011 One time the page is load when submitting the form. Is it possible to avoid this page load by a method other than <form> to set the cookie? Or I need to use AJAX to set the cookie without page load? Quote Link to comment https://forums.phpfreaks.com/topic/246680-set-and-update-cookie-by-a-select-form/#findComment-1266700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.