我刚开始使用PHP。目前正在使用简单的信息显示系统。
如果我在一个文件PageWithMessages.php
中有以下语句:
<span class="msg">
<?php
if( $tableChange == true ) { echo "Table saved. ";}
else if ( $rowChange == true ) { echo "Record saved. ";}
else if ( $rowDelete == true ) { echo "Record deleted. ";}
else if ( $rowNew == true ) { echo "New record created. ";}
?>
</span>
如何使用以下退出语句从另一个文件传递该变量(e.g. $rowNew = true )
的信息?
if($result) {
header("location: PageWithMessages.php");
// set $variable = true ??
exit();
} else {
die("Query failed");
}
任何建议都非常感谢。
答案 0 :(得分:2)
如果我理解你的问题:
if($result) {
header("location: PageWithMessages.php?var=1");
exit();
}else {
die("Query failed");
}
然后在PageWithMessage.php上:
$var = (isset($_GET['var'])?true:false); // will be true
答案 1 :(得分:2)
你可以有两个函数,一个叫setMessage()
,一个getMessage()
:
function setMessage($message)
{
$_SESSION['message'] = $message;
}
function getMessage()
{
if (isset($_SESSION['message']) {
$message = $_SESSION['message'];
unset($_SESION['message']);
return $message;
}
return false;
}
然后像这样使用它:
if($result) {
setMessage('Row changed');
header("location: PageWithMessages.php");
// set $variable = true ??
exit();
}else {
die("Query failed");
}
<?php if ($message = getMessage()): ?>
<span class="msg">
<?php echo $message; ?>
</span> #
<?php endif; ?>
或者您可以使用$ {GET变量(url params),例如header("location: PageWithMessages.php?rowChanged=1");
并查找$_GET['rowChanged'];
答案 2 :(得分:1)
include("your_file_including_variables_and_statements.php");
或
echo htmlspecialchars($_GET['variable']);
在PageWithMessages.php?variable = hello 的情况下
答案 3 :(得分:1)
有几种可能性:
1)将其作为GET参数发送 2)将其存储在会话中 3)包括第二个脚本
简短示例如何:
@ 1)在第一个文件中:
//replace XXXX accordingly with tableChange, rowChange, rowDelete or rowNew
header("location: PageWithMessages.php?action=XXXX");
和PageWithMessage.php
<span class="msg">
<?php
if( $_GET['action'] == "tableChange" ) { echo "Table saved. ";}
else if ( $_GET['action'] == "rowChange" ) { echo "Record saved. ";}
else if ( $_GET['action'] == "rowDelete" ) { echo "Record deleted. ";}
else if ( $_GET['action'] == "rowNew" ) { echo "New record created. ";}
?>
</span>
@ 2)请参阅HERE或在header()之前的第一个文件中:
session_start();
$_SESSION['tableChange'] = true/false;
//same for other vars
和PageWithMessages.php内部
<span class="msg">
<?php
session_start();
if( $_SESSION['tableChange'] == true ) { echo "Table saved. ";}
else if ( $_SESSION['rowChange'] == true ) { echo "Record saved. ";}
else if ( $_SESSION['rowDelete'] == true ) { echo "Record deleted. ";}
else if ( $_SESSION['rowNew'] == true ) { echo "New record created. ";}
?>
</span>
@ 3)在第一个文件而不是标题中:
$tableChange = true/false;
//same for other vars
include(dirname(__FILE__) . '\PageWithMessages.php');
答案 4 :(得分:0)
使用SESSION
据我所知,你想要这样的东西
session_start();
$_SESSION['varaible'] = true;
if($result) {
header("location: PageWithMessages.php");
exit();
}else {
die("Query failed");
}
session_start();
$sessionVariable = $_SESSION['varaible'];
<span class="msg">
<?php
if( $sessionVariable == true ) { echo "Table saved. ";}
?>
</span>