1

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

}

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

ok great, i will try it now, one question though i have this $mailErrorDisplay = 'display:none;'; at the very very top of the php which sets the default value, so the error doesn't display until there is one, how do i make this work with the session variables? Because at the moment left at it is, the error just constantly displays whereas before it didn't
You can say: $mailErrorDisplay = (isset($_SESSION['mailErrorDisplay'])) ? "display: visible" : "display:none";
Hi, just tried that and it still shows the error, I added your above comment code at the top like you said. This is what ihave in the php if ($sql_recipient_num == 0){ $_SESSION['mailErrorMsg'] = "<u>ERROR:</u><br />The Recipient does not exist.<br />"; $_SESSION['mailErrorDisplay'] = ''; header('Location: mail.php?tid=3'); } and this is what i have in the css #mail_errors {height: 30px; width: 767px; <?php /*?><?php print "$mailErrorDisplay";?><?php */?> <?php echo $_SESSION['mailErrorDisplay']; unset($_SESSION['mailErrorDisplay']); ?> }
1
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.

Comments

1

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');
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.