alex777 alex777 -4 years ago 204
PHP Question

PHP variable scope between php files

I have a form which creates a shipment and stores it in my database.
Upon success I want to print the tracking number and a QR Code based on the number.
Unfortunately the scope of my variables can't reach the .php file I want.
I have tried 2 things as you can see in the comments but in both attempts the scope seems to be the problem.
Any ideas?

shipment.php code:



<?php
include 'core/init.php';
include 'includes/overall/kat_start.php';

if (empty($_POST) === false)
{
$required_fields = array('city', 'destination', 'time', 'cost', 'type');
//echo '<pre>', print_r($_POST, true), '</pre>';
$field_length = array('city', 'destination');

foreach($_POST as $key=>$value)
{
if(empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = 'Fields marked with an asterisk are required.';
break 1;
}
}

if(empty($errors) === true){
foreach($_POST as $key=>$value){
if(strlen($value) > 30 && in_array($key, $field_length) ===
true){
$errors[]='The size of one of your entries is not
acceptable. Entry size must be smaller than 30 characters.';
}
}

}
}

?>
<h1>Create Shipment</h1>

<?php
if(isset($_GET['success']) && empty($_GET['success'])){
echo 'You have created a shipment succesfully';
//echo $register_data['trnumber'];
//This doesn't work, I get "variable 'register_data' undefined error!
}
else
{
if(empty($_POST) === false && empty($errors) === true)
{
$GLOBALS['trnumber'] = tracking_number($_POST['city'],
$_POST['destination']);
$register_data = array
(
'trnumber' => $trnumber,
'city' => $_POST['city'],
'destination' => $_POST['destination'],
'time' => $_POST['time'],
'cost' => $_POST['cost'],
'type' => $_POST['type'],
);
shipment_submit($register_data);
header('Location: qrcode.php');
}
else{echo output_errors($errors);}
?>
<form action="" method="post">
<ul>
<li>
Starting City*:<br>
<input type="text" name="city">
</li>
<p><li>
Destination*:<br>
<input type="text" name="destination">
</li></p>
<p><li>
Time*:<br>
<input type="text" name="time">
</li></p>
<p><li>
Cost*:<br>
<input type="text" name="cost">
</li></p>
<p><li>
Type*:<br>
<input type="text" name="type">
</li></p>
<p><li>
<input type="submit" value="Submit">
</li></p>

</ul>

</form>

<?php
}
include 'includes/overall/end.php'; ?>


================
qrcode.php code:

<?php
include 'core/init.php';
//logged_in_redirect();
include 'includes/overall/kat_start.php';
//include 'shipment.php';
//This include creates errors, it shouldnt be here
?>

<h2>You created the shipment successfully</h2>

<?php
echo $GLOBALS['trnumber'];
//This doesn't work I get "undefined index 'trnumber'" error
?>

Answer Source

One solution to your problem is using $_SESSION

<?php 
session_start();
/*session is started if you don't write this line can't use $_SESSION */
$_SESSION["trnumber"]=$value;

echo $_SESSION["trnumber"];
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download