php - 从 mailer.php 提交的表单将转到空白页面。可能重定向到上一个 html 页面?

标签 php email redirect

我的主页上有一个表格。它可以很好地发送消息,并且我可以通过电子邮件收到它们,但是如何让 mailer.php 重定向到我网站的主索引页面?目前是mailer.php;

<?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 = "removed for privacy";

        // Set the email subject.
        $subject = "New contact from $name";

        // 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.";
    }

?>

该表格位于我网站主索引页的底部,如果可能的话,我希望它自动返回那里。这是 HTML:

<div class="form">

                        <div id="sendmessage">Your message has been sent. Thank you!</div>
                        <div id="errormessage"></div>
                    <form id="ajax-contact" method="post" action="mailer.php">
    <div class="field">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>
    </div>

    <div class="field">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
    </div>

    <div class="field">
        <label for="message">Message:</label>
        <textarea id="message" name="message" required></textarea>
    </div>

    <div class="field">
        <button type="submit">Send</button>
    </div>
</form>

最佳答案

成功时使用header()重定向到另一个页面。

关于 header() 的 PHP 手册: https://www.php.net/manual/en/function.header.php

// Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        //Now redirect to your index.php page and display your success message on your index page. 
        $url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
        $url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
        $url .= 'index.php?success';  // <-- Your relative path with a success post through url             
        header('Location: ' . $url, true, 302);
        exit;
    } 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.";
    }

然后在索引页上使用

if(isset($_GET['success'])){ 
    $success = "Thank You! Your message has been sent."; // Display this where you want your user to see it.
} 

显示成功消息

关于php - 从 mailer.php 提交的表单将转到空白页面。可能重定向到上一个 html 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60610208/

相关文章:

php - 如何将集合传递给可邮寄的 laravel

PHP header ();可靠性

java - Jersey - 使用 Get Not Put 重定向,导致重定向循环

php - UTF8 中的特殊字符 mailto : subject= link and Outlook

php - 重定向而不改变url

PHP/HTML : Creating A SubMenu

javascript - 如何更改 .done(function(data) 下的响应?

javascript - 亚马逊支付与 Javascript 和 PHP 的集成 - PaymentPlanNotSet

php - 我在 MySQL 表中添加了一列,但没有显示任何数据

html - 电子邮件 HTML 签名;当我改变表格大小时字体大小改变