0
<?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,但页面本身没有显示任何内容。

4

2 回答 2

4

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

echo $_GET['message'];

请参阅变量范围。

于 2013-08-05T16:26:02.357 回答
1

emailnotsent.php

<?php
session_start();

echo $_GET['message'];
?>

emailsent.php

<?php
session_start();

echo $_GET['message'];
?>
于 2013-08-05T16:26:16.787 回答