0 レビュー
2 回答
phpファイル間のPHP変数スコープ
貨物を作成してデータベースに保存するフォームがあります。 成功したら、追跡番号とその番号に基づいたQRコードを印刷したいと思います。 残念ながら、私の変数のスコープは、必要な.phpファイルに到達できません。 コメントでわかるように、私は2つのことを試しましたが、どちらの試みでもスコープが問題のようです。 何かアイデアはありますか?
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'; ?>
<?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
?>
わからない
0
レビュー
答え :
解決策:
問題の解決策の1つは、$_SESSIONを使用することです
<?php
session_start();
/*session is started if you don't write this line can't use $_SESSION */
$_SESSION["trnumber"]=$value;
echo $_SESSION["trnumber"];
?>
わからない
0
レビュー
答え :
解決策:
これの代わりに
'trnumber' => $trnumber,
試してみる
'trnumber' => $GLOBALS['trnumber'],
そしてqrcode.phpコードでは、 $trnumber
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。