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!
You're missing the name of the POST
parameter in your AJAX call. It should be:
data: { data: 'moderne'}
or
data: 'data=moderne'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.