dongritan5654 2017-10-30 18:32
浏览 94
已采纳

提交后PHP重定向到HTML页面[关闭]

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!

  <?php

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');
       exit();
   }

 }


?>


                    <?php
        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']), '"';}?>>
                            <br>
                            <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']), '"';}?>>
                            <br>
                            <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>
                            <br>
                            <input type='submit' value='Submit' id='submit-button'><br>
                        </form>


                        <?php
            }

            ?>
  • 写回答

1条回答 默认 最新

  • dreljie602951 2017-10-30 20:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?