What i am trying to do is to pass a value, using a variable, from one page using php to another page using php and then pass this value in the second page from php to javascript.
I initialize the variable in HTML depending on which image i click. The problem is that every time i pass a different value(different values are 12 and 7 in the code below), meaning that i clicked on a different image,and after reloading my page, i still get the first value.
I tried unsetting the variable and/or session_destroy() but with no luck. To be more clear:
First page:
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="secondpage.php"><img src="images/first_image.png" alt="image" <?php $_SESSION['choice'] = 12; ?>></a>
<a href="secondpage.php"><img src="images/second_image.png" alt="image" <?php $_SESSION['choice'] = 7; ?>></a>
</body>
</html>
Second page:
<?php
session_start();
$option = $_SESSION['choice'];
//unset($_SESSION['choice']); //failed
echo $option; //option is always 12
//session_destroy(); //failed
?>
<!doctype html>
<html>
<head>
<title>Monuments</title>
<script>
var option = <?php echo json_encode($option); ?>;
//var option = "<?php echo $option; ?>"; // i tried also this way
alert("option is : " + option); //option is always 12
</script>
</head>
<body>
</body>
</html>
Can you tell me what is the problem? Or recommend any better solutions for what i want to do?