Search code examples
phphtmlscopexmlhttprequestglobal-variables

Loading PHPs via XMLHTTPRequest don't share the same variable scope


I understand the concept of global variables in PHP and understand the pros/cons regarding the use of global variables. Nevertheless, I've decided to use them, but I'm running into issues regarding the scope and visibility of them.

Situation:

Depending on the selection of a menu I'm loading different PHPs into a div. The PHPs require the same common set of data, which I want to avoid to be reloaded and kept in memory all the time for each PHP. In the example below the GlobalVars.php will be kept in memory twice and will also fetch the data from the database twice.

The problem is, that by loading them into the div, they don't share the scope of the main.html. The global variables in GlobalVars.php can be seen and accessed by the code in another.php, but not in PHP1.php, nor in PHP2.php.

GlobalVars.php:

<?php
    $var1 = "*";
    $var2 = 5;
    // Various SQL fetches
?>

Main.html:

<?php require_once="./GlobalVars.php"; ?>
<?php require_once="./another.php"; ?>

<script>
    function LoadHTML(href) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", href, false);
        xmlhttp.send();
        return xmlhttp.responseText;   
    }

    switch(menuitem) {
        case 0: break;
        case 1: document.getElementById("contentdiv").innerHTML=LoadHTML("./PHP1.php") break;
        case 2: document.getElementById("contentdiv").innerHTML=LoadHTML("./PHP2.php") break; break;
        case 3: break;
        default:
    }
</script>

PHP1.html:

<?php
    require_once="./GlobalVars.php";
    // code ...
?>

PHP2.html:

<?php
    require_once="./GlobalVars.php";
    // code ...
?>

The question is, how can I load the PHPs into a div and 'see' and work with the variables in the scope of main.html?

Regards

Carsten


Solution

  • I solved the problem by not loading PHP1 and PHP2 through JS, but earlier in the PHP engine run. Instead of loading the PHPs into the same DIV I now load them into different DIVs. The visibility of those DIVs is then being controlled through JS later on.

    The variable $LastScreen is being pulled from an SQL DB and contains the last screen shown, so that the user gets the same screen up that he had before he reloaded the page.

    Generation of the DIVs:

    <html>
        <body>
            <div class="myclass" id="screen1"
                <?php if (strcmp($LastScreen, "screen1") !== 0) {echo " style=\"display:none; \"";} ?>
            >
                <?php require_once './PHP1.php'; ?>
            </div>
            <div class="myclass" id="screen2"
                <?php if (strcmp($LastScreen, "screen2") !== 0) {echo " style=\"display:none; \"";} ?>
                >
                <?php require_once './PHP2.php'; ?>
            </div>
        </body>
    </html>
    

    Switching screens in JS:

    <script>
        function SwitchScreen (screen){
            var arr = document.getElementsByClassName('myclass');
            var i;
            for (i=0; i < arr.length;i++) {
                arr[i].style.display = "none";
                }
            document.getElementById(screen).style.display = "inline";
    
            // push screen name to SQL
            // ...
        }
    </script>
    

    Regards

    Carsten