在PHP中的另一个页面上打印时,变量不会显示

时间:2013-08-05 16:23:58

标签: php email

<?php
session_start();

 error_reporting(E_ALL);
// Clean up the input values 
foreach($_POST as $key => $value) {  
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
}

$name = trim(strip_tags(stripslashes($_POST['name'])));
$email = trim(strip_tags(stripslashes($_POST['email'])));
$subject = trim(strip_tags(stripslashes($_POST['subject'])));
$message = trim(strip_tags(stripslashes($_POST['message'])));

$successful = "Message was sent";
$failure = "Message was not sent";

$header = "From: $email\r\n";

$validEmail = filter_var($email, FILTER_VALIDATE_EMAIL);

if (isset($name) && isset($email) && isset($subject) && isset($message)) {
    if ($validEmail) {
        mail ("myemail@email.com", $subject, $message, $header);
        header("Location: emailsent.php?message=$successful");
    }
    else {
        header("Location: emailnotsent.php?message=$failure");
    }
}
?>

emailnotsent.php:

<?php
session_start();

echo $failure;
?>

emailsent.php:

<?php
session_start();

echo $successful;
?>

我看到“已发送消息”的URL,但页面本身没有显示任何内容。

2 个答案:

答案 0 :(得分:4)

没有显示任何内容,因为在emailsent.php中未定义$ successful。尝试打印GET变量:

echo $_GET['message'];

参见 Variable Scope.

答案 1 :(得分:1)

emailnotsent.php

<?php
session_start();

echo $_GET['message'];
?>

emailsent.php

<?php
session_start();

echo $_GET['message'];
?>