Home > Software engineering >  localStorage element not updating after header
localStorage element not updating after header

Time:06-20

I'm currently developing a personal project of a japanese language learning website. The user's progress is stored inside a localStorage variable. The problem I faced is that after setting the variable's value and placing the header to a different page, the localStorage variable doesn't update. If the header is removed it works fine. Any help would be very much appreciated. Please follow a snippet of my code. Thank you!

if (!in_array($id, $_SESSION['seenRight'])) {
    array_push($_SESSION['seenRight'],$id);
    if (count($_SESSION['seenRight']) == 10) {
        ?> <script> 
            if (typeof(Storage) !== "undefined") {
                    <?php
                    // Check if there was already a localstorage for that level, make a percentage with the new added value, replace value.
                    // Lock the level.
                        if(count($_SESSION['seenWrong']) == 0) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "50");<?php
                        }
                        if(count($_SESSION['seenWrong']) > 0 && count($_SESSION['seenWrong']) <= 2) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "40");<?php

                        }
                        if(count($_SESSION['seenWrong']) > 2 && count($_SESSION['seenWrong']) <= 4) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "30");<?php

                        }
                        if(count($_SESSION['seenWrong']) > 4 && count($_SESSION['seenWrong']) <= 6) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "20");<?php

                        }
                        if(count($_SESSION['seenWrong']) > 6 && count($_SESSION['seenWrong']) <= 8) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "10");<?php

                        }
                        if(count($_SESSION['seenWrong']) > 8 && count($_SESSION['seenWrong']) <= 10) {
                            ?> localStorage.setItem("progressLevel"   "<?php echo $_SESSION['level'] ?>", "0");<?php
                        }
                        unset($_SESSION['level']);
                        unset($_SESSION['id']);
                        $_SESSION['seenRight'] = array();
                        $_SESSION['seenWrong'] = array();
                        header('Location:review_menu.php'); // IF THIS IS REMOVED, IT WORKS FINE.
                    ?>
            } else {
                <?php header('Location:review_menu.php'); ?>
            }
        </script> <?php
    }
}

CodePudding user response:

placing the header to a different page

...you said it yourself. You tell the browser to go to another page...so this page, the one you're outputting the localstorage code into, never gets loaded by the browser and so of course the JavaScript never executes.

If you want to do a redirect, I suggest doing it from javascript (e.g. using window.location), after the localstorage code has had chance to execute.

  • Related