简体   繁体   中英

undefined index while trying to echo a variable $_SESSION, php jquery ajax,

edit i am not getting the error undefined index 'time' any longer. Now i am not getting any errors, but when i try echoing $timezone; nothing happens.

what am i missing here that is causing the undefined index 'time'? i do have jquery in the page.

index.php

<?php
session_start();
$timezone=$_SESSION['time'];

?>

script, using jquery to get the time and then setting it as a $_SESSION['time'] variable.

<script>
$(document).ready(function() {
    if("<?php echo $timezone; ?>".length==0){
        var visitortime = new Date();
        var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
        $.ajax({
            type: "GET",
            url: "<?php echo $_SERVER['DOCUMENT_ROOT'].'/social_learning/php/timezone.php';?>",
            data: 'time='+ visitortimezone,
            success: function(){
                location.reload();
            }
        });
    }
});
</script>

timezone.php

 <?php 
  session_start();
  $_SESSION['time'] = $_GET['time'];
 ?>

just change $timezone=$_SESSION['time']; to $timezone=isset($_SESSION['time'])?$_SESSION['time']:"";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM