使用标题:位置时变量不生效

时间:2012-05-22 19:45:04

标签: php html css

我有这个php部分,如果在当前状态下为true,则用户会被发送回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');,这允许我确保用户正在查看错误所在的选项卡,则不会发生上面列出的任何变量,因此不会显示错误。是否有其他形式的标题:我可以使用的位置?

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

}

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

您认为header()命令的作用类似于require_once(),其中新脚本被“注入”当前脚本。 它实际上是向浏览器发送了一个名为“Location:mail.php?tid = 3”的http标头。然后浏览器通过重定向到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');
} 

答案 2 :(得分: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']);

那应该能满足你的需求。

相关问题