views:

29

answers:

1

Hi,

I am new to PHP and would like to know how to setup and use Cookies to store user menu selections between pages.

Basically, when a user say clicks on a menu option called "About Us", I would like to somehow store this selection via a cookie that I could then use somewhere else?

FYI, I am using the jquery .click function to determine which menu option is selected, i.e.

        $("ul.sf-menu li a").click(function() {
        var menu_opt = $(this).attr("href");
    });

Thanks.

+1  A: 

First of all you need setcookie

<?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>

and then you need to retrieve it using:

   echo $_COOKIE['TestCookie'];

To set a classdepending on cookie:

<a href="about"  <?php if($_COOKIE['TestCookie'] === "About"){ echo "class='selected'";}?>>About page</a>

The idea hereis to check the value of our cookie and ifits what we want then weecho the class assignation. There are much better ways of achievingwhat you want without the use of cookies.

If this is not what you want you will have to reword that question much better because as it is I am taking guesses.

Iznogood
Thanks Iznogood but could you pls assist on how I can pass the "this" value so that I can set the correct class on the newly selected menu option.
tonsils
Thanks for the update. The bit that I am unsure how to do is set the cookie based on the menu selection I have made - see my updated question above wth regards to jQuery .click() call. How can I in jQuery set php $value to javascript menu_opt value above?
tonsils
Hi again, in addition to my previous comment, what other ways could I achieve this without cookies?
tonsils
@tonsils Kind of hard to say without knowing how your code is setup. Accept this answer and post another question about this subject. You will get help and wont make a mess of this question here.
Iznogood
Will do. Could you pls assist with my second last comment. Thanks
tonsils
@tonsils I just told you to post anew question for that. You will get better answers. Here no one but me know you asked that.
Iznogood
Just realised you added stuff. reading it now
Iznogood