0

After a week of trial and error with lots of reading here, I still can't get this working.
What I want to do: I have a website with 24 product pages. Each product is available in "classic" or "moderne". Each page will have a clickable button to switch between one and the other (display a different img, essentially). If a user clicks to see Product A in "moderne", then moves on to Product B, I want them to see the "moderne" version directly without having to click again (the default display being "classic"). I think SESSION is exactly what I need.
My code (I AM using session_start() at the top of both pages):

Product A page:

<?php $_SESSION["collection"] = ""; ?> <script> function ClasstoMod(){ $(".classique, .moderne").toggle(); // toggles the images, it's working } </script> <script> $(document).ready(function() { $( "#display-moderne" ).click(function() { request = $.ajax({ url: "classtomod.php", type: "post", data: 'moderne' }); // callback handler that will be called on success request.done(function (response, textStatus, jqXHR){ // log a message to the console console.log("Hooray, it worked!"); }); // callback handler that will be called on failure request.fail(function (jqXHR, textStatus, errorThrown){ // log the error to the console console.error( "The following error occured: "+ textStatus, errorThrown ); }); }); }); </script> <html> <div id="display-moderne" class="classique" href="#" onclick="ClasstoMod();">Check out the new Moderne Line!</div> <div id="display-classique" style="display:none;" class="moderne" href="#" onclick="ClasstoMod();">Return to our Classique Line!</div> </html> 

classtomod.php

<?php $_SESSION['collection'] = $_POST['data']; ?> 

Product B page, for now I'm only trying to echo the right "collection", I'll work on the JS to display the proper image later!

<?php echo "Collection = " .$_SESSION['collection']; ?> 

The console log is successful, however the echo on Page B does not echo the new SESSION. If I run it as shown, it simply echoes "Collection = "
If I define $_SESSION['collection'] = "test" on Page A, Page B echoes "Collection = test" as expected, so that part is working.
What I want is, after I click on #display-moderne is for Page B to echo "Collection = moderne"

I think I'm close, any help is appreciated!

9
  • You're missing session_start(); in the PHP scripts. Commented Apr 7, 2016 at 21:23
  • You might want to look into local storage or cookies also unless you really need a reason to have the server hold the session. Commented Apr 7, 2016 at 21:27
  • @Barmar I have session_start at the top of all pages, before the doctype. Commented Apr 7, 2016 at 21:27
  • @Rob, do you think cookies are easier to "make it work"? or is just better practice for this? Commented Apr 7, 2016 at 21:29
  • I would say better practice. There's really no reason for the server to hold this session when it's based on a users preference. Local storage or a cookie would be just fine, however, local storage compatibility, although better, is not used in all browsers. Commented Apr 7, 2016 at 21:31

1 Answer 1

1

You're missing the name of the POST parameter in your AJAX call. It should be:

data: { data: 'moderne'} 

or

data: 'data=moderne' 
Sign up to request clarification or add additional context in comments.

1 Comment

well, there you go, I knew it was something simple. THANK YOU! works great, now to display the right divs based on the session.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.