Home » Php » PHP global variable shared between two files

PHP global variable shared between two files

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have two different files in the same folder, the first one is called “form.php” the second one is called “add.php”, I can’t get the value of the global variable in “form.php”

“form.php”

<?php
include("add.php");
global $Resultat;
echo $Resultat;
?>

“add.php”

<?php
global $Resultat;
if (isset($_POST["submit"])) {
    if (empty($_POST["val_1"]) || empty($_POST["val_2"])) {
        header("Location: form.php?FillTheForm");
        exit();
    } else {
        $Resultat=$_POST["val_1"]+$_POST["val_2"];
        echo "$Resultat"; //this works if I don't use header (the next instruction).
        header("Location: form.php"); // I want to redirect and still echo the value in "form.php".
    }
}

If I don’t use the header in the else statement it echos the value in “add.php” but i want to redirect to “form.php” and echo it there.

How to&Answers:

You said that you want to redirect to “form.php” and echo it there.

echo ‘ window.location = “add.php” ‘;

Do this instead of INCLUDE.