Jump to content

How to pass a user's select value to a cookie and maintain it?


hitman47

Recommended Posts

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:

<?php
session_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 by hitman47
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.