php文件之间的PHP变量范围

时间:2017-05-20 16:52:24

标签: php scope

我有一个表格可以创建货件并将其存储在我的数据库中。 成功后,我想根据号码打印跟踪号码和QR码。 不幸的是,我的变量范围无法达到我想要的.php文件。 我已经尝试了两件事,你可以在评论中看到,但在两次尝试中,范围似乎都是问题所在。 有什么想法吗?

shipment.php代码:

<?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代码:

<?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 个答案:

答案 0 :(得分:2)

您的问题的一个解决方案是使用$ _SESSION

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

echo $_SESSION["trnumber"];
?>

答案 1 :(得分:0)

而不是这个

'trnumber'     => $trnumber,

尝试使用

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

qrcode.php 代码中,将其作为$trnumber

进行访问