我有一个PHP脚本,它使用Jquery存储PHP SESSION变量。
脚本说" Hooray它工作了#34;但会话变量GotTheUserTZ和UserTimeZone没有存储 - 因为当我重新加载页面时,我不断得到" Undefined index:UserTimeZone"从行:
echo $_SESSION['UserTimeZone'];
我无法弄清楚这个问题。
谢谢:)
<?php
// HTML HEADER AND BODY CODE IN ANOTHER FUNCTION
session_start();
// ALL OF BODY HTML OUTPUTTED HERE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
if (!isset($_SESSION['GotTheUserTZ']))
{
?>
<script type="text/javascript">
$(document).ready(function() {
var gmtRe = /GMT([\-\+]?\d{4})/;
var d = new Date().toString();
var tz = gmtRe.exec(d)[1];
request = $.ajax({
url: "<?php echo $GLOBALS['SITEURL'].'storetimezoneajax.php' ?>",
type: "POST",
data: "vv="+tz
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
alert("Hooray, it worked!");
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
alert(
"The following error occurred: "+
textStatus, errorThrown
);
});
});
</script>
<?php
echo $_SESSION['UserTimeZone'];
}
?>
</body>
</html>
和storetimezoneajax.php在同一目录中:
<?php
$_SESSION['GotTheUserTZ'] = "yes";
$_SESSION['UserTimeZone'] = $_POST['vv'];
?>
我在localhost上运行,如果这有所不同。
答案 0 :(得分:0)
更改此
的数据参数数据:{ vv:tz }
再试一次。
答案 1 :(得分:0)
感谢您的所有回复。正如上面所建议的那样,缺少session_start()是问题所在。