Hello!
I'm trying to pass a variable from one .php file to another, through the use of a global variable. However, whenever I make a variable "global", the page does not load. Here is the snippet of code that is causing the problem. If more context is required, let me know.

<?php
include 'tablegen.php';
if (empty($_POST['term'])) {
header("location:landing.php");
}
if(isset ($_POST['term'])) {
$x = $_POST['term'];
     connect();
     global $result=tableGen($x);

}//end main if




echo "<form action='email.php' method = 'post'>";
echo "<p><b>Do you want this in an email?</b></p>";
echo "<input type='text' name='send'>";
echo "<input type='submit' name='submit' value='Send!' />";
echo "</form>";

error_reporting(-1); // display all faires
ini_set('display_errors', 1);  // ensure that faires will be seen
ini_set('display_startup_errors', 1); // display faires that didn't born

?>

Here is the file that is supposed to "catch" the global variable.

<?php

if(isset($_POST['send'])) {
sendEmail($result); //this would work if result was global.. I think? 
}
function sendEmail($table) {
$email = $_POST['send'];
$headers = array(
'From: summitmathguide@gmail.com',
'Content-Type: text/html',
'Content-Type: text/css',
);

$content = $table;

mail($email,'HTML Email',$content,implode("\r\n",$headers));
echo "<p>Email Sent!</p>";



}
?>
Member Avatar for LastMitch

However, whenever I make a variable "global", the page does not load. Here is the snippet of code that is causing the problem. If more context is required, let me know.

You need to include this in your file:

session_start();

On your top file it should have this:

session_start();
globdb();

and your bottom file it should have this also:

session_start();
globdb();

Then the function should be this:

function globdb(){
global $result;
$result = $_POST['term'];
}

Thanks for the answer. I'm new to PHP, so I'm not sure in which files I should put the first (session_start), and where should I define the globdb function? Also, 'term' comes from a pervious form, that gets passed into tableGen(), which creates a table.

Member Avatar for LastMitch

Thanks for the answer. I'm new to PHP, so I'm not sure in which files I should put the first (session_start), and where should I define the globdb function? Also, 'term' comes from a pervious form, that gets passed into tableGen(), which creates a table.

Created a new php file and called it function.php

Then put this:

include('database.php'); 
function globdb(){
global $result;
$result = $_POST['term'];
}

in it.

Then the 2 files that you posted above on top of each file put this:

session_start();
include('database.php'); 
include("function.php");
globdb();

to test rather it works then try to echo it out

with this:

echo globdb();

You can read mroe about global here:

http://www.php.net/manual/en/reserved.variables.globals.php

Thanks for the answer, but now, the top file does not even load. Also, can you explain line 3 in the first snippet? Result should be equal to the output of tableGen().

Member Avatar for LastMitch

Thanks for the answer, but now, the top file does not even load. Also, can you explain line 3 in the first snippet? Result should be equal to the output of tableGen().

I don't know what you are talking about? Can you post what you got on top of those 2 files?

line 3?

You mean this:

include('database.php');
function globdb(){
global $result;
$result = $_POST['term'];
}

database.php file is your database connection file which you need to have on top of each file

What is tableGen()? What is in it?

Can you post the the code?

Sure! This, "landing.php" takes a post, and sends it to "search.php" which sends it to "tablegen.php". "tablegen.php" creates the table, and stores the table in a variable $output. Essentially, I want to pass $output from "tablegen.php" to "email.php", so I can send the table created in tableGen() in an HTML email.

Here is "landing.php"

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== FALSE) {
header("location:redir.html ");
}

?>
<html>
<title>Search</title>
<head>
<style>
body {
    margin:50px 0px; padding:0px;
    text-align:center;
    background-color: #1C2932;
    }

#login {
    width:500px;
    margin: 0px auto;
    text-align:center;
    padding:15px;
    border:1px dashed #333;
    font-family: Helvetica;
    background-color:#C0C0C0;
    }

</style>
</head>
<body>
<div id="login">
<img src="/resources/logo.png">
<form action="search.php" method="post">
<p><b>Enter Student ID:</b></p> 
<input type="text" name="term" />
<input type="submit" name="submit" value="Go!" />        
</div>

</body>
</html>

This is the updated "search.php" with your code.

<html>
<head>
<style type="text/css">
table {
background-color: #C0C0C0;
}

th{
width: 150px;
text-align:center;
border-style: solid;
border-width: 2px;
border-color: black;
background-color: #008080;
font-family: Helvetica;
}
td {
border-style: solid;
border-width: 2px;
border-color: black;
font-family: Helvetica;
background-color: #FFFF00;
text-align:center;
}
body {
background-color:#1C2932;
}

h1 {
font-family: Helvetica;
font-size: 24px;
color: #989898;
}

p {
font-family: Helvetica;
font-size: 18px;
color: #989898;
}

</style>
</head>
<body>

<?php
session_start();
include 'pass.php';
include 'tablegen.php';
if (empty($_POST['term'])) {
header("location:landing.php");
}
if(isset ($_POST['term'])) {
$x = $_POST['term'];
     connect();
     $result=tableGen($x);
     globdb();
}//end main if



echo "<form action='email.php' method = 'post'>";
echo "<p><b>Do you want this in an email?</b></p>";
echo "<input type='text' name='send'>";
echo "<input type='submit' name='submit' value='Send!' />";
echo "</form>";

error_reporting(-1); // display all faires
ini_set('display_errors', 1);  // ensure that faires will be seen
ini_set('display_startup_errors', 1); // display faires that didn't born

?>
<br></br>
<form method="LINK" action="landing.php">
<input type="submit" value="Go Back!">
</form>

</body>
</html>

This is the new "email.php" updated with your code.

<html>
<head>
<style>
body {
background-color:#1C2932;
}
p {
font-family: Helvetica;
font-size: 18px;
color: #989898;
}

</style>
</head>
<body>
<?php
session_start();
include 'pass.php';
if(isset($_POST['send'])) {
echo globdb();
}
function sendEmail($table) {
$email = $_POST['send'];
$headers = array(
'From: summitmathguide@gmail.com',
'Content-Type: text/html',
'Content-Type: text/css',
);

$content = $table;

mail($email,'HTML Email',$content,implode("\r\n",$headers));
echo "<p>Email Sent!</p>";

error_reporting(-1); // display all faires
ini_set('display_errors', 1);  // ensure that faires will be seen
ini_set('display_startup_errors', 1); // display faires that didn't born

}
?>
</body>
</html>

Also, "search.php" now loads fine, but when I enter something into the email field, it takes me to a blank "email.php" page, when it should show the table.

Ah looks like you edited some of your replies. Sorry for not noticing those earlier. By line 3 I meant the string that says $result = $_POST['term']; I was confused by this because $result=tableGen();.

Member Avatar for LastMitch

Also, "search.php" now loads fine, but when I enter something into the email field, it takes me to a blank "email.php" page, when it should show the table.

You need to include this:

include 'tablegen.php'; 

in your email.php file.

Since tablegen.php is passing into it.

Thanks again. However, tablegen is returning to "search.php" (at least I think), and "search.php" is sending this to "pass.php" which is called by "email.php." After including what you told me, I am getting the error saying that $result in "pass.php" is undefined. This is probably because it doesn't know the value of $result? Also, what is the purpose of $result = $_POST['term']; The query from landing.php has something to do with this? Here is pass.php.

<?php
function globdb() {
global $result;
$result= $_POST['term'];

}


?>

EDIT: On second thought, is it easier just to send the output from tablegen.php to email.php ?

Member Avatar for LastMitch

This is probably because it doesn't know the value of $result?

OK

When you post this:

if(isset ($_POST['term'])) {
$x = $_POST['term'];
connect();
global $result=tableGen($x);

That's why I did this:

global $result;
$result= $_POST['term'];

Where is $_POST['term']; located?

It was undefined because $result doesn't have $_POST['term']; in it.

You want the landing.php file pass to search.php file then to email.php file

I assume you put this:

include('database.php');
function globdb(){
global $result;
$result = $_POST['term'];
}

in the pass.php file?

What is this:

if(isset ($_POST['term'])) {
$x = $_POST['term'];
connect();
$result=tableGen($x);
globdb();
}//end main if

What is connect();?

What is tableGen($x);?

Is there a function tableGen($x) and if there is then where is it located?

connect() is a function located in tablegen.php that does the job of connecting to the SQL server, pretty much the same thing as database.php. tableGen($x) is a function in tablegen.php, that takes $x which is the post from landing.php. Also, is there a reason why I can't just include 'email.php' in tablegen.php, and call sendEmail($output) ?

Member Avatar for LastMitch

connect() is a function located in tablegen.php that does the job of connecting to the SQL server, pretty much the same thing as database.php. tableGen($x) is a function in tablegen.php, that takes $x which is the post from landing.php.

So you want tablegen.php to be on email.php?

What are you doing here:

<?php
function globdb() {
global $result;
$result = $_POST['term'];
}

You define $x = $_POST['term']; in the other files then on your pass.php it's $result = $_POST['term'];

I mean change it $result to all $x.

Hm. I've tried sending $output directly from "tablegen.php" to "email.php"

tablegen.php

include 'email.php';

//generate table, etc.



echo $output;
sendEmail($output);

email.php

<html>
<head>
<style>
body {
background-color:#1C2932;
}
p {
font-family: Helvetica;
font-size: 18px;
color: #989898;
}

</style>
</head>
<body>
<?php
function sendEmail ($table) {
if(isset($_POST['send'])){
$email = $_POST['send'];
$headers = array(
'From: summitmathguide@gmail.com',
'Content-Type: text/html',
'Content-Type: text/css',
);


$content = $table;

mail($email,'HTML Email',$content,implode("\r\n",$headers));
echo "<p>Email Sent!</p>";
error_reporting(-1); // display all faires
ini_set('display_errors', 1);  // ensure that faires will be seen
ini_set('display_startup_errors', 1); // display faires that didn't born
}
}
?>
</body>
</html>

Now, when I run the page, search.php loads fine. However, when I enter my email, and click send. It sends me to a blank "email.php" and no email appears. The email doesn't send for some reason. This method seems like it would work?

Member Avatar for LastMitch

However, when I enter my email, and click send. It sends me to a blank "email.php" and no email appears. The email doesn't send for some reason. This method seems like it would work?

Did you fixed the other issue?

Meaning the $result and $x.

On here:

mail($email,'HTML Email',$content,implode("\r\n",$headers));

What is $_POST['send']; it should be email not send.

When you fill out a form which should look like this:

<form action="email.php" method="POST">
<p>Email</p> <input type="text" name="email">
<br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

then on your email.php it should look like this:

<?php
$email = $_POST['email'];
$mailheader = "From: $email \r\n";
$recipient = "your email"; // this is where the $email (email address) will send to your email (email address)
mail($recipient, $mailheader) or die("Error!");
?>

Do you understand how this works now. The code I provided above works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.