doukou4066 2014-01-14 21:49
浏览 59

单击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

  • 写回答

2条回答 默认 最新

  • duanjuelian4640 2014-01-14 21:52
    关注

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

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题