单击div设置php会话变量

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

Since you already are referencing jQuery, you can use $.get().

function setSessionGame(str){
    $.get("setsessiongame.php?q="+str, function(data) {
        // ajax complete
    });
}

Try and make the ajax call in sync but use jquery as you are including it:

function setSessionGame(str){
    $.ajax({
        type: "GET",
        url: "setsessiongame.php",
        async: false,
        data:  {
            "q" : str   
        },
        success: function(msg) {
        }
    });
)

and see if it changes something.

Maybe it could be that the entire querystring is not passed properly.

Mind you this is just a test to try and understand where things are going wrong as your code seems properly formatted.