1

我有这个 php 部分,如果在当前状态下为真,则用户将被发送回 mail.php 并且两者都$mailErrorMsg可以$mailErrorDisplay正常工作。

php原版

if ($sql_recipient_num == 0){

    $mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
 $mailErrorDisplay = ''; 

} 

以及改变的css部分

#mail_errors {
height: 30px;
width: 767px;
text-align: center;
color: #666666;
font-family: Verdana, Geneva, sans-serif;
font-size: 9px;
clear: both;
font-weight: bold;
<?php print "$mailErrorDisplay";?>  
background-color: #FFF; 
border: thin solid <?php print "$mail_color";?>;

}

但是,如果我添加此行header('Location: mail.php?tid=3');以确保用户正在查看错误所在的选项卡,则上面列出的变量都不会发生,因此错误不会显示。还有其他形式的 header:location 我可以使用吗?

if ($sql_recipient_num == 0){
     header('Location: mail.php?tid=3');
    $mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
 $mailErrorDisplay = ''; 

}
4

3 回答 3

2

使用标头不会传递任何这些变量。你应该做的是使用会话。

session_start(); // put this on the top of each page you want to use
if($sql_recipient_num == 0){
    $_SESSION['mailErrorMsg'] = "your message";
    $_SESSION['mailErrorDisplay'] = "whatever";
    // header
}

然后在您要打印这些错误消息的页面上。

session_start();
print $_SESSION['mailErrorMsg'];
// then you want to get rid of the message
unset($_SESSION['mailErrorMsg']; // or use session_destroy();
于 2012-05-22T19:48:36.060 回答
1

您认为 header() 命令的行为类似于 require_once(),其中新脚本被“注入”到当前脚本中。它实际上是向浏览器发送一个 http 标头,上面写着“Location: mail.php?tid=3”。然后浏览器通过重定向到 mail.php 页面来遵守,有点像点击一个链接。

您拥有的任何内容仍将在后台运行,但人员浏览器现在已转到新页面。如果您想传递 $mailErrorMsg 和/或 $mailErrorDisplay,您需要将它们存储在会话变量或 cookie中,并将这些声明放在您的标头重定向上方,如下所示:

if ($sql_recipient_num == 0){
     $mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
     $mailErrorDisplay = ''; 
     header('Location: mail.php?tid=3');
} 
于 2012-05-22T19:47:29.353 回答
1
header('location: mail.php');

将浏览器重定向到该页面。然后所有变量都为空。我会使用会话变量来存储信息。

session_start(); //must be before any output
if ($sql_recipient_num == 0){
    header('Location: mail.php?tid=3');
    $_SESSION['mailErrorMsg'] = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
    $_SESSION['mailErrorDisplay'] = ''; 
}

然后当你想显示:

session_start(); //must be before any output
echo $_SESSION['mailErrorMsg']; unset($_SESSION['mailErrorMsg']);

那应该可以为您提供所需的东西。

于 2012-05-22T19:52:46.377 回答