更改与阿贾克斯会话变量变量、阿贾克斯

2023-09-10 20:57:36 作者:柠檬爱人i

欧凯所以我不是很了解如何做到这一点,但是这是我想要做的事情。我需要更新所使用的网站上繁衍场所的会话。我这样做与阿贾克斯但是当我改变会话它仍然作为Ajax调用之前相同的网站。这里有一个例子:

Okey so I can't quite see how to do this but this is what I am trying to do. I need to update a SESSION that is used on multiply places on the website. I do this with ajax but when I change the SESSION it remains the same in the website as before the ajax call. here is an example:

的index.php

index.php

<?php
session_start();
?>

<script>
function sessionUpdate()
{ 
//Do a get ajax call that send the 2 parameters to updateSession.php
}
</script>

<?php
$_SESSION['foo'] = "foo"; 
$_SESSION['bar'] = "bar"; 

echo"<div>";
echo"<p  onclick="sessionUpdate()>Update Session</p>";

echo"{$_SESSION['foo']} {$_SESSION['bar']}";
echo"</div>";
?>

updateSession.php

updateSession.php

<?php
session_start();

$_SESSION['foo'] = "new foo"; 
$_SESSION['bar'] = "new bar";
?>

现在,该会话使用人在现场,所以我不能只替换从示例DIV用的innerHTML = data.responseText Ajax调用的信息;只是在那个地方。反正当我做foo和酒吧会不会改变这个德呼应,是他们无法在没有重新加载页面更改或有什么问题,只是静态变量?

Now, the sessions are used al over the site so I can't just replace the information from the ajax call in the example div with a innerHTML=data.responseText; at just that place. Anyway when I do this teh echo of the foo and bar sessions don't change, are they just static variables that can't be changed without a page reload or what is the problem?

推荐答案

据我了解,你打开的index.php 键,例如div包含的会话变量的默认值(在你的榜样'富巴)。当您单击更新会议,做的innerHTML = data.responseText 你可以看到更新后的会话值(新富新巴根据示例)。但在重新加载的index.php - 这表明'富巴了。根据你的code,你不检查,如果你要设置默认会话变量。尝试替换您的index.php

As far as I understand, you open index.php and example div contains default values of session variables ('foo bar' in your example). After you click Update Session, doing innerHTML=data.responseText you can see updated session values ('new foo new bar' according to example). But after you reload index.php - it shows 'foo bar' again. According to your code, you do not check if you should set default session variables. Try to replace in your index.php

<?php
$_SESSION['foo'] = "foo"; 
$_SESSION['bar'] = "bar"; 

echo"<div>";

通过

<?php
if(!isset($_SESSION['foo']))
   $_SESSION['foo'] = "foo"; 
if(!isset($_SESSION['bar']))
   $_SESSION['bar'] = "bar"; 

echo"<div>";

更新code将检查会话变量被设置(在第一次使用开放式的index.php)。一旦未设置 - 默认值将被分配,但是一旦做到这一点,它不会覆盖任何未来的变化(所以,以后的Ajax调用foo和bar变量将设置和你的code不会重写它的值)

Updated code will check if session variable is set (user open index.php at the first time). Once it is not set - default values will be assigned, but once that is done, it will not override any future changes (so, after ajax call foo and bar variables will be set and your code will not rewrite its values)