I have a simple JS script (below) that on click (of the element with an id of #addClass) adds a class (.switcher) to the body tag. The script uses jquery.
I would like to know if it's possible using PHP to store that info and keep it intact when the user moves on to the next page within the site (ie. the added class remains in place)? In fact the class needs to stay in place until the element with an id #removeClass has been clicked no matter how many pages the user chooses to browse through. Please be gentle with your responses - I'm not much of a coder!

<script type="text/javascript">
     
    $("#addClass").click(function () {
    $('body').addClass('switcher');
    });
    $("#removeClass").click(function () {
    $('body').removeClass('switcher');
    });
    </script>

I am posting this on the PHP section of Daniweb following a response to this post (originally on the JS section)http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/414040

Airshow mentions one solution might be possible using PHP $_session var. I don't know anything about PHP $_session var or how one would go about implementing it for my needs. I hope someone can help me out here...

Thanks

Daniel

Recommended Answers

All 7 Replies

Member Avatar for diafol

Using sessions will probably mean having to use ajax. You can probably avoid all that by using normal cookies.

Every time you change the class - overwrite the cookie (check to see if it exists first! if not create it and give it a value).

You don't even have to call the php $_COOKIE var. Just keep it all in js.

I agree with ardav. I, personally, am not well versed with transitioning JS data to $_SESSION although I am good with sessions otherwise. But when you're using JavaScript and JavaScript can easily write to a cookie I don't see any reason to muddy things up by adding a 'middleman' of trying to get PHP to "understand" the JS to be able to send it to the client's SESSION.

The only reason that comes to mind right now to use SESSION over COOKIES is if your visitor has cookies disabled. But then you can add a message to the page that uses the JS/COOKIE stating that they need to have cookies enabled, at least for your domain, to have the proper experience. If they refuse to then they'll just have to keep setting the class each time. You could perhaps put up a DHTML "tool tip" box to explain that the cookie that the site needs to set stores only a piece of information necessary for that part of the site to work. No other data personal, or otherwise, is contained. This could save room on the actual page but would still make it explicit that cookies need to be turned on for it to work on the page itself.

Best of luck.

Thanks for your postitive responses. I feel like I'm getting closer to a solution. However, I have no idea how to even create a cookie let alone use one to store the relevant info that will make my clients website work. Would someone be kind enough to hold my hand through this one and give me a concrete code example of the cookie file? Thanks. Dan...

Member Avatar for diafol

This is javascript - go to the js forum OR - go over to the jQuery site and search for cookie retrieval and writing. There is a dedicated plugin for this. I'm not going to copy and paste that info here.

Member Avatar for diafol

OK, mark this one solved - for future reference, perhaps it would be better to ask a mod to move the post rather than start a new one.

Oops - I didn't realise that I could move a post. Marked as solved now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.