简体   繁体   English

提交电子邮件表单后尝试在 PHP 中重定向

[英]Trying to redirect in PHP after submitting an email form

I am building my first site using an MVC framework, and the view that the user sees is determined by the $_GET['page'] variable.我正在使用 MVC 框架构建我的第一个站点,用户看到的视图由 $_GET['page'] 变量决定。 If you would like to see other code I can post it, but as far as I can tell, the code that is having the problem is displayed below.如果您想查看其他代码,我可以发布它,但据我所知,出现问题的代码如下所示。 I'm not sure if it makes a difference, but this code is held in a different folder from the actual form itself, and all applicable files are included via the index folder and the include() command.我不确定它是否有所不同,但此代码与实际表单本身保存在不同的文件夹中,并且所有适用的文件都通过 index 文件夹和 include() 命令包含在内。

I am trying to redirect the user to a different page after submitting an email form, and have tried both header() and using window.location in javascript, but I am new to PHP and cant seem to get it to work, it just submits the form and sends the email.我试图在提交电子邮件表单后将用户重定向到不同的页面,并尝试了 header() 和在 javascript 中使用 window.location,但我是 PHP 新手,似乎无法让它工作,它只是提交表格并发送电子邮件。

<?php

    ob_start();

    $error = ""; $successMessage = "";

    if ($_POST) {

        if (!$_POST["email"]) {

            $error .= "An email adress is required.<br>";

        }

        if (!$_POST["subject"]) {

            $error .= "A subject is required.<br>";

        }

        if (!$_POST["contactMessage"]) {

            $error .= "A message is required.<br>";

        }

        if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
  
            $error .= "A valid email adress is required.<br>";

        }

        if ($error != "") {

            $error = '<div class="contactFormElement infoMessage" role="alert"><p><strong>There were error(s) in your form:</strong></p>' . $error . '</div>';

        } else {

            $emailTo = "example@example.com";

            $subject = $_POST['subject'];

            $contactMessage = $_POST['contactMessage'];

            $headers .= "From: Mailer <mailer@example.com";

            $headers .= " Reply-To: ".$_POST['email'];
            
            $headers .= " Return-Path: ".$_POST['email'];

            if (mail($emailTo, $subject, $contactMessage, $headers)) {

                header('Location: index.php');
                exit();

            } else {

                $error = '<div class="contactFormElement infoMessage"><p><strong>Your message was not sent. Please try again later.</strong></p></div>';

            }

        }

    }

    ob_end_flush();

?>

I have tried to put header() where it is currently located in the code, as well as trying this javascript in its place我试图将 header() 放在代码中当前所在的位置,并在其位置尝试使用此 javascript

echo '<script type="text/javascript">
           window.location = "http://www.google.com/"
      </script>';

When I echo the javascript, all that it does is submit the form and display window.location = "http://www.google.com/" on the screen, it doesn't seem to actually execute the script, and with the header() command, it just exits the page, it doesn't redirect it.当我回显 javascript 时,它所做的只是提交表单并在屏幕上显示 window.location = "http://www.google.com/",它似乎并没有真正执行脚本,并且使用header() 命令,它只是退出页面,它不会重定向它。

Any help would be greatly appreciated, thank you!任何帮助将不胜感激,谢谢!

This answer is simply for anyone looking at this question in the future.此答案仅适用于将来查看此问题的任何人。 I was able to get the header();我能够得到header(); function to work, but it required reworking how my entire site redirects between pages, so I wouldn't take anything that is displayed here very seriously, as this is my first attempt at coding anything by myself and has a lot of flaws.功能正常工作,但它需要重新设计我的整个网站如何在页面之间重定向,所以我不会非常认真地对待这里显示的任何内容,因为这是我第一次尝试自己编码任何东西并且有很多缺陷。

Add ob_end_flush();添加 ob_end_flush(); before header('..');在标题之前('..');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM