I would like to have users fill out my contact form, hit submit and then be redirected to my success.html page that thanks them for their submission. Everything worked great when I did this on a local Apache server while I was developing my site. Now that I put it up on the web, hitting submit no longer redirects to the success.html page, instead it takes them straight back to index.php which I don't want. I tried using the php headers and I also tried using JS with window.location to redirect users as suggested in similar Stack Overflow questions but neither worked. Can someone help me here? Thank- you!


if (empty($_POST) === false) {
    //errors array
    $errors = array();
    //message variables
    $name = $_POST['form-name'];
    $email = $_POST['form-email'];
    $message = 'This is a message from: ' . $name . '.' . "

" . 'Please send your reply to: ' . $email . '. ' . "

" . $_POST['form-msg'];
    $headers = 'From: ' . $email;

    //Validation for name, email, and message fields
    if (empty($name) === true || empty($email) === true || empty($message) === true) {
        $errors[] = 'Name, email, and message are required!';

    } else {
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            $errors[] = 'That\'s not a valid email address.';
        if (ctype_alpha(str_replace(' ', '', $name)) === false) {
            $errors[] = 'Name must only contain letters!';


   if (empty ($errors) === true) {
       //send email
       mail('myemail@gmail.com', 'Contact Form Submission', $message, $headers);
       //redirect user
       header('Location: http:/www.mysite.com/success.html');



        if (isset($_GET['sent']) === true) {
            header('Location: success.html');

        } else {
            if (empty($errors) === false) {
                echo '<ul>';
                foreach($errors as $error) {
                    echo '<li>', $error, '<li>';


                        <form method='post' action='success.html'>
                            <label for='form-name'>Name: </label>
                            <input type='text' name='form-name' id='form-name' placeholder="Type your name..." required <?php if (isset($_POST[ 'form-name'])===true) { echo 'value="', strip_tags($_POST[ 'form-name']), '"';}?>>
                            <label for='form-email'>Email: </label>
                            <input type='email' name='form-email' id='form-email' placeholder="Type your email..." required <?php if (isset($_POST[ 'form-email'])===true) { echo 'value="', strip_tags($_POST[ 'form-email']), '"';}?>>
                            <label for='form-msg'>Msg: </label>
                            <textarea type='text' name='form-msg' id='form-msg' placeholder="You get the drift..." required <?php if (isset($_POST[ 'form-msg'])===true) { echo 'value="', strip_tags($_POST[ 'form-msg']), '"';}?>></textarea>
                            <input type='submit' value='Submit' id='submit-button'><br>


duandui2803 我想到了。我将PHP代码移动到单独的文件submit.php中,然后将action属性设置为该页面,以便表单提交将通过我的PHP代码运行。然后,我设置标题以将用户重定向到我的success.html页面,显示“感谢您的提交”。这是问题,而不是使用php页面。如果你能提交答案,我想帮助你。
接近 3 年之前 回复
duanji4870 谢谢,我会继续努力。当它在本地服务器上时,我的一切工作正常。我不确定托管有什么影响,但谢谢你的帮助!
接近 3 年之前 回复
duanpa5237 对不起,你显然需要花一些时间弄清楚你写的所有内容到底是做什么的。这就是为什么大多数体验开发人员会主张分离关注点(即尽可能地分割PHP和HTML)。做一些调试,看看你在哪里侧身,实际到达了什么代码,以及变量的值是什么。
接近 3 年之前 回复
duankeng1911 Patrick,所以我只是将表单操作更改为index.php,允许再次发送电子邮件,但现在它不会重定向到success.html页面。
接近 3 年之前 回复
dongshao1021 <formmethod='post'action='success.html'>将表单提交给success.html,而不是index.php
接近 3 年之前 回复
dongningwen1146 JayBlanchard->这是一个文件,一个index.php文件,是我的主页。我只包括表单和PHP代码,而不是我在网站上的所有其他HTML。还有另一个文件,success.html,我没有在这里包含,因为它只是“谢谢你联系我”。文本和返回索引的链接。@JackGal我修复了这个URL,现在它正在重定向,但是电子邮件没有像按下提交那样发送到我的收件箱。
接近 3 年之前 回复
dongzhuang6417 答案与PHP根本没有关系。它必须如何构建URL。你的网址错了。header()函数内的http部分应为:http://
接近 3 年之前 回复
douqiao1413 关于你所包含的代码流是什么,目前还不清楚。这一切都在一个档案中吗?多个文件?什么是文件名(或名称)?我看到多个.html文件的引用,但没有引用任何.php文件。你的PHP代码实际上是如何运行的?
接近 3 年之前 回复
douxian9010 谢谢,是的,我现在看到了。我修复了它,现在它正在重定向,但是当我填写表单时,联系表格电子邮件不再发送。
接近 3 年之前 回复
dsgoj7457 http:/www.mysite.com你错过了第二个/。看起来像现代浏览器(至少我目前的Chrome版本)给你一些宽容并为你解密它,但它在技术上是不正确的
接近 3 年之前 回复
douji5397 看这里http:/www。看到什么遗失?
接近 3 年之前 回复
douyin4875 以什么方式?你可以再详细一点吗?除了在这个联系表单中使用PHP之外,我对PHP知之甚少,对于我的无知感到抱歉。谢谢!
接近 3 年之前 回复
doudi8519 如果这是您的实际代码,而不是手动转录的复制品,则您的header()网址中会出现拼写错误
接近 3 年之前 回复


这里有一些问题。 第一个是问题中实际提出的那个。 解决方法是将标题('Location:http:/www.example.com/success.html'); </ code>更改为标题('Location:http://www.example。 com / success.html'); </ code>(我知道你实际上有 mysite.com </ code>,但我们不允许在答案中使用它。</ p>

第二个问题是表单发布到HTML页面时(几乎可以肯定)发布到PHP页面。&lt; form method ='post'action ='success.html'&gt ; </ code>。既然你说你提供的所有代码都在index.php中,我建议&lt; form method ='post'action ='index.php'&gt; </ code> </ p>

也就是说,将所有这些代码放在一个文件中是很难遵循的,往往会导致混淆和错误。遵循关注点分离,您应该将表单处理代码拆分成一个新文件,该文件只是决定如何处理提交的da 并采取适当行动。 任何HTML都应该尽可能地放在自己的文件中。</ p>

如果我不得不猜测,我会说这在测试环境中实际上并没有真正发挥作用。 相反,错误可能只是掩盖了。</ p>
</ div>



There are a few issues here. The first is the one that is actually posed in the question. The solution to this is changing header('Location: http:/www.example.com/success.html'); to header('Location: http://www.example.com/success.html'); (I know that you actually had mysite.com, but we're not allowed to use that in answers.

The second issue is that the form is posting to an HTML page, when it should (almost certainly) be posting to a PHP page. <form method='post' action='success.html'>. Since you said that all of the code you provided was in index.php, I suggested <form method='post' action='index.php'>

That said, having all this code together in one file is quite hard to follow and tends to lead to confusion and errors. Following the principle of separation of concerns, you should split your form-handling code out into a new file that simply decides what to do with the submitted data and acts appropriately. Any HTML should be in its own file as much as possible.

If I had to guess, I'd say that this never actually worked 100% in your testing environment. Instead, the errors were probably just masked.

Csdn user default icon