即使我刚刚打开我的网站,php电子邮件也会自动显示通知

时间:2018-01-29 07:01:46

标签: php notifications

我在编码方面只是初学者,不能判断。

我想在点击提交按钮后显示通知,或者在我用表单输入的数据出现问题时显示通知。但即使我刚刚打开文件,似乎已经显示了通知。

以下是代码:

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "myemail@gmail.com";

    // Set the email subject.
    $subject = "Customer Inquiry";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);

        echo " Thank You! Your message has been sent.";
    } else { 
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}
?>    

以下表格:

<div class="column">
    <h3>Get in Touch</h3>
    <form action="#" target="myIframe" id="contactUs" method="post">

        <div class="field half first">
            <label for="name">Name</label>
            <input name="name" id="name" type="text" placeholder="Name">
        </div>
        <div class="field half">
            <label for="email">Email</label>
            <input name="email" id="email" type="email" placeholder="Email">
        </div>
        <div class="field">
            <label for="message">Message</label>
            <textarea name="message" id="message" rows="6" placeholder="Message"></textarea>
        </div>
        <input name="submit" type="submit" value="Send Message">
    </form>
    <?php include "mailer.php"?>
</div>    

2 个答案:

答案 0 :(得分:0)

而不是

if ($_SERVER["REQUEST_METHOD"] == "POST") {

你应该

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {

isset($_POST['submit'])检查表单是否已提交。

答案 1 :(得分:0)

由于最后else阻止,您收到错误。当您显示表单时,它是GET请求,而不是POST,因此if条件为false。这不应被视为错误,您应该只在出现错误时显示表单。

摆脱

 else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}