0

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
?>
2
  • If these are two separate HTTP requests then anything in-memory will start anew in the second request. You could use something like $_SESSION to persist a value between requests. Commented May 20, 2017 at 16:58
  • When you use a header() redirect you are actually reloading the page. The reason you can't pass variables between the two is that they are in no way connected. To pass a variable through here you would need to add anything you want passed through into a query string on the end of your url and then get it from the _GET. You could also use a session and store things in _SESSION if you prefer. Commented May 20, 2017 at 17:00

2 Answers 2

2

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"];
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Yup, then you can use $_SESSION["trnumber"]; in all your other .php files. I would suggest making sure you unset that variable when it is no longer needed/required.
Thanks for the suggestion!
0

Instead of this

'trnumber'     => $trnumber,

Try With

'trnumber'     => $GLOBALS['trnumber'],

And in qrcode.php code access this as $trnumber

2 Comments

This idea does not work. I get undefined variable error.
Can you able to print value of $GLOBALS['trnumber'] before redirecting to qrcode.php?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.