Pass variables from one Script to another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SecretAgentX9
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 1:44 pm

Pass variables from one Script to another

Post by SecretAgentX9 »

I'm new to PHP, only about 3 months in. I'm trying to take a form a user fills out, check the $_POST variables to make sure all is good and if it is then I want to send those variables to a new page to be displayed. If there are Errors then it displays it above the form. Right now I have two outside scripts that check to make sure the email is a valid one and then if all is good another script to mail those variables. On to the code

Code: Select all

require('validemail.php');
require('mailsuccess.php');

$Errors = '';

if(isset($_POST['submit']))
{

$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Phone'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$ConfirmEmail = Trim(stripslashes($_POST['ConfirmEmail']));
$Hdd = Trim(stripslashes($_POST['Hdd']));
$Warranty = Trim(stripslashes($_POST['Warranty']));
$Message = Trim(stripslashes($_POST['Message']));
$Captcha = Trim(stripslashes($_POST['Captcha']));

if (validemail($Email)){

// Make sure the required Fileds are completed and the email addresses match
if (empty($Name)) { $Errors .= "\n Please enter your name."; }
if (empty($Tel)) {$Errors .= "\n Please enter your telephone number."; }
if ($Email !== $ConfirmEmail) {$Errors .= "\n Your email addresses don't match."; }
if ($Captcha !== '4') {$Errors .= "\n The Captcha is incorrect."; }
if (empty($Errors)) {

success($Name, $Tel, $Email, $Hdd, $Warranty, $Message);

/* Here is where I'm trying to figure out how to send the variables to the new page to be displayed */
}
}
else { $Errors .= "\n Please enter a valid email."; }
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Pass variables from one Script to another

Post by AbraCadaver »

Code: Select all

session_start();  // if your sucess() function outputs anything then this line will need to be before success()
$_SESSION = compact('Name', 'Tel', 'Email', 'Hdd', 'Warranty', 'Message'); // this is kinda sloppy, you can also do
//$_SESSION['Name'] = $Name;
//etc...
header('Location: http://example.com/yourpage.php');
exit;
Then as the first line of yourpage.php you need:

Code: Select all

session_start();
Then use the variables:

Code: Select all

$Name = $_SESSION['Name'];
echo $name;
//or just
echo $_SESSION['Name'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
SecretAgentX9
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 1:44 pm

Re: Pass variables from one Script to another

Post by SecretAgentX9 »

Thank you!
Post Reply