doutui7955 2014-10-30 08:47
浏览 35

两次重新加载页面后会话变量变化为什么?

Main.php

<?php
session_start();
msgbox("Are you sure?", "confirm");
$Result = $_SESSION['id'] ;
print "<p id='txt'> </p>";
if($Result == 1 )
    echo "Result is true";
elseif ( $Result == 2 ) 
    print "<br><br>Return value is '$Result'";
function msgbox($msg, $type)
{
    if ($type == "confirm")
    {
        print "<script src='Validate.js'></script>";
        print "<script> ret = confirm ('$msg') ;
        if (ret)
            Write_to_variable('1');
        else
            Write_to_variable('2');
        </script>";
?>

Validate.js

function Write_to_variable( value ) 
{
    if (window.XMLHttpRequest) 
    {
        xmlhttp=new XMLHttpRequest();
    }
    else 
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txt").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","Variable.php?id="+value,true);
    xmlhttp.send(); 
}    

Variable.php

<?php
session_start();
if ( $_GET['id'] ) 
{
    $_SESSION['variable'] = $_GET['id'];
    print $_SESSION['variable'];
}
?>

What I am trying to do is,

Getting the value from the javascript confirm box and store return value of confirm box to php SESSION variable using Ajax. In Ajax File I just assigning $_SESSION['id']= $_GET['id']. When the program get execute, I pressed OK from the confirm box. It assign 1 to the session variable of id. If I pressed cancel next time the value doesn't changed, when I am tried to pressed cancel another time, the value of session have changed to id as 2. What is the problem, any suggestions and solution are kindly accepted.

I cant find what is logic hidden behind it. I can clearly see what is returns and what ajax returns but the session variable used in the main.php is changing after second click.

Thanks in Advance.

  • 写回答

2条回答 默认 最新

  • dth62818 2014-10-30 09:24
    关注

    seems there's nothing wrong just try

    1. console log your java script parameters and check for errors

    2. use alert and check what what value is passed to the ajax file

    3.use die() in your ajax file and check for mismatch.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度