Ajax不会改变我的$ _SESSION变量

时间:2013-12-05 23:34:16

标签: php ajax session session-variables

我有一个带有$_SESSION变量的main.php页面,我调用ajax来修改这个var,在ajax上$_SESSION var发生了变化但是当我在main.php上检查它时它没有'吨。

我每个session_start(); session_id();上的ID都相同。

如果重新加载页面,则修改值

main.php

<?php session_start();echo session_id();$_SESSION['equipo_g']=0 ?>
...
<!--SCRIPT CALLED BY BUTTON-->
<script>
function pdf(){ 

if (window.XMLHttpRequest){
  xmlhttp=new XMLHttpRequest();
  }
else{
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("pdf").innerHTML=xmlhttp.responseText;
    }
  } 

var e = document.getElementById("equipos_g");
var equipo = e.options[e.selectedIndex].value;

xmlhttp.open("GET","test.php?r="+equipo,true);
xmlhttp.send();
}
</script>
...
<!--Trigger a func to call the ajax-->
<button type="button" onclick="pdf();">Preparar Grafica</button>
...
<div id"pdf"></div>
...

test.php的

<?php 
session_start();
$r=$_GET["r"];

$_SESSION['equipo_g']=$r;
echo $_SESSION['estacion_g'];//The change is done

//Run a script that use $_SESSION['equipo_g']
echo '<button type="button" onClick="mostrar_graf1();" id="bot_grafica">Graficar</button>';
exit();
?>

2 个答案:

答案 0 :(得分:0)

尝试发送您的会话ID以及您的请求:

xmlhttp.open("GET","test.php?r="+equipo+"&sid=<?php echo session_id(); ?>",true);

并在开始会话之前使用它来设置会话ID:

session_id($_GET['sid']);
session_start();

另请注意,如果您致电

$_SESSION['equipo_g']=0

在每次加载main.php时,您在尝试阅读之前将会话变量equipo_g设置为零。

答案 1 :(得分:0)

最后,我只需将所有需要的值发送到ajax并在php中执行操作,然后使用JSon将参数等结果发送到JScript函数! 我不想使用它,但它现在有效,感谢SquareCat:3 (这是代码):

test.php的

<?php
$estacion=$_GET["estacion"];
$equipo=$_GET["equipo"];
$year=$_GET["year"];
$month=$_GET["month"];
$week=$_GET["week"];

$alldata=array();

//SOME MYSQL OPERATIONS USING THE VAR's PRODUCING $result_data

array_push($alldata,$result_data);
?>

<button type="button" onClick='mostrar_graf2(<?php echo  json_encode($alldata) ?>);' id="bot_grafica">Graficar</button>