hitman47 Posted June 18, 2013 Share Posted June 18, 2013 (edited) I have a table with some data. The user chooses how he wants the data in the table to be sorted in the user.php page. The table data are stored in my mysql database. Easy. However because the table is paginated i have a huge problem. Everytime i change the page, the sortment is lost. I used cookies. <form method="post" action="news_list.php" onchange="form.submit()"> <select name="sortid" > <?php $sort_array = array(' ','date desc','date asc','title desc','title asc') ; for($i=1; $i<5; $i++) { if($i ==$shortchoice ){ echo '<option selected="selected" value="'.$i.'">'.$sort_array[$i].'</option>'; } else { echo '<option value="'.$i.'">'.$sort_array[$i].'</option>'; } } echo '</select></form>'; ?> In the beggining of news_list.php, before the header i place this: <?phpsession_start(); setcookie('sortid', $_POST['sortid']);?> Now i have a paginated sorted table that keeps in $_cookie['sortid'] the users sorting choise When i change the first page IN the table, the sortment is maintained, because the cookie keeps its value. After i browse more pages, more than two IN the sorted table, the cookie looses its value and the table loses it's sortment, it returns to the default sorting value Should i place <?php session_start(); setcookie('sortid', $_POST['sortid']); ?> in my user.php page or in news_list.php? I have tried both but the problem insists. How to maintain the cookie value? Please i have racked my brains to find a solution, and nothing seems to work. Any suggestions? HELP!!! Edited June 18, 2013 by hitman47 Quote Link to comment https://forums.phpfreaks.com/topic/279304-how-to-pass-a-users-select-value-to-a-cookie-and-maintain-it/ Share on other sites More sharing options...
DavidAM Posted June 20, 2013 Share Posted June 20, 2013 session_start and setcookie are two unrelated actions. You do not need the session for the cookie (I can't tell if you are using a session for something else in the code, but you don't need it for the cookie). The problem is that you are unconditionally setting the cookie. If you come back to the page, through a link, it sends a GET request, and the $_POST array is empty, so you are setting the cookie to an empty string. You need to test for the POST value before setting the cookie: if (isset($_POST['sortid'])) setcookie('sortid', $_POST['sortid']); P.S. Please use tags when posting code. Quote Link to comment https://forums.phpfreaks.com/topic/279304-how-to-pass-a-users-select-value-to-a-cookie-and-maintain-it/#findComment-1437125 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.