首页 > 解决方案 > 使用 PHP 邮件处理程序提交表单后如何刷新页面

问题描述

通过 PHP 邮件处理程序指令从联系表单提交数据后,即使电子邮件发送正常,整个页面也会变为空白。按“F5”键刷新页面,但我宁愿系统自动执行此操作,而不是打扰用户。

我尝试在 if(isset) 行之后立即使用 header 函数,如下所示:

[code]
if(isset($_POST['submit']))
{
  header("Location: https://amacwebsolutions.com");
}
[/code]

我是否将重定向代码放在错误的位置,还是需要完全不同的解决方案?

这是完整的 PHP 代码:

[code]
<?php
    if(isset($_POST['submit'])) {   
        {
          header("Location: https://amacwebsolutions.com");
        }    
        $name=$_POST['name'];
        $email=$_POST['email'];
        $subject=$_POST['subject'];
        $msg=$_POST['msg']; 
        $to='info@amacwebsolutions.com';
        $subject="RE: ".$subject;
        $message="Name: ".$name."\n"."Wrote the following: "."\n\n".$msg;
        $headers="From: info@amacwebsolutions.com\r\nReply-To: $email"; 
        if(mail($to, $subject, $message, $headers)){
            echo "<h1>Sent succesfully! Thank you,".$name." We will contact you shortly!</h1>";
        }  
            else {
               echo "Something went wrong!";
            }
    }
?>
[/code]

标签: php

解决方案


通过将标头置于 if 条件中来尝试此操作。

if(isset($_POST['submit'])) {   

    $name=$_POST['name'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $msg=$_POST['msg']; 
    $to='info@amacwebsolutions.com';
    $subject="RE: ".$subject;
    $message="Name: ".$name."\n"."Wrote the following: "."\n\n".$msg;
    $headers="From: info@amacwebsolutions.com\r\nReply-To: $email"; 
    if(mail($to, $subject, $message, $headers)){
        echo "<h1>Sent succesfully! Thank you,".$name." We will contact you shortly!</h1>";
        header("Location: https://amacwebsolutions.com");
    } else {
           echo "Something went wrong!";
        }
}

推荐阅读