I'm stuck trying to pass variables between 2 PHP files a.php
and b.php
using require()
. I learned that PHP variables have a single scope, which means that if a.php
includes b.php
, it can read its variables. However, I would like b.php
to be able to read a variable from a.php
with my current setup. For example, I can print out $msg
if it is set in b.php
but not a.php
. Is there a simple way to do this?
a.php
<?php
$msg = "Welcome NEW USER ! We can't wait for you to check out our books!";
require("connect.php");
require("b.php");
$table = "SELECT * FROM `login`";
if ($query_run=mysqli_query($cnn,$table)){
$username = $_POST['username'];
$pw = $_POST['password'];
$sql = "INSERT INTO login (username,password) VALUES ('{$username}','{$pw}')";
if($cnn->query($sql)===TRUE){echo "Query ran successfully";}
else{echo "Error: ".$sql."<br>".$cnn->error;}
}
else{die("table connection failed");}
$cnn.close();
?>
b.php
<?php
echo "<h1>".$msg."</h1>
<form action='' method='post'>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' name='submit'>
</form>
"
?>