I am trying to set up a system whereby if a user clicks on a div, a php session variable should be set to the value of another session variable, which has been defined previously. For example, if a user clicks on the div with id = "game2", $_SESSION['game'] should be set to equal $_SESSION['game2'], which is already defined.
I'm new to ajax and have been having a lot of trouble accomplishing this. Here is what I currently have:
listofgames.php:
<?php session_start(); ?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function setSessionGame(str){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "setsessiongame.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php echo "<a href=\"#\" onclick = \"setSessionGame(". $_SESSION['game2'] .")\">" ?>
<div id = "game2"></div>
</a>
</body>
</html>
setsessiongame.php:
<?php
session_start();
$q = $_GET['q'];
$_SESSION['game'] = $q;
?>
This code has been turning $_SESSION['game'] into an empty string instead of setting it equal to $_SESSION['game2'] when the div is clicked. I would really appreciate any help fixing my code.
Thanks very much