1

以下の現在の状態で true の場合、ユーザーが mail.php に送り返され、両方が正しく機能するこの 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() のように機能すると考えています。実際には、「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');
} 
于 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 に答える