Al Hennessey
Al Hennessey

Reputation: 2445

Variables not taking effect when using header:location

I have this php section that if is true in the current state below the user gets sent back to mail.php and both $mailErrorMsg and $mailErrorDisplay work correctly.

The php original

if ($sql_recipient_num == 0){

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

} 

And the css part that changes

#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";?>;

}

However if I add this line header('Location: mail.php?tid=3'); which allows me to make sure the user is looking at the tab the error is in, none of the variables listed above take place and therefore the error does not show. Is there any other form of header:location I can use?

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

}

Upvotes: 1

Views: 123

Answers (3)

JoeCortopassi
JoeCortopassi

Reputation: 5093

You're thinking that the header() command acts like require_once() where the new script is 'injected' into the current script. It is actually sending an http header to the browser that says "Location: mail.php?tid=3". The browser then complies by redirecting to the mail.php page, kind of like clicking on a link.

Anything you have under it will still run in the background, but the persons browser is now off to the new page. If you want to pass $mailErrorMsg and/or $mailErrorDisplay, you'll need to store them in a session variable or cookie, and place those declarations above your header redirect like so:

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

Upvotes: 1

Pitchinnate
Pitchinnate

Reputation: 7556

header('location: mail.php');

Redirects to the browser to that page. All variables are then empty. I would use a session variable to store the info.

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'] = ''; 
}

Then when you want to display:

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

That should get you what you need.

Upvotes: 1

Marcus Recck
Marcus Recck

Reputation: 5065

Using a header won't pass any of those variables. What you should do is use a session.

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
}

Then on your page where you want to print these error messages.

session_start();
print $_SESSION['mailErrorMsg'];
// then you want to get rid of the message
unset($_SESSION['mailErrorMsg']; // or use session_destroy();

Upvotes: 1

Related Questions