【问题标题】:Page not redirecting after PHP contact form submissionPHP联系表单提交后页面未重定向
【发布时间】:2021-06-28 13:53:29
【问题描述】:

我正在尝试创建一个发送电子邮件的联系表单。我在主页上有一个简单的版本,它可以工作并重定向到感谢页面。当我尝试在联系页面上运行相同的 PHP 脚本时,它会发送表单但不会重定向到感谢页面。

发送电子邮件和重定向的 index.html:

<section class="appointment-one" id="booking">
    <div class="container wow fadeInUp"  data-wow-duration="1.5s">
        <div class="inner-container">
            <h3 class="appointment-one__title" >Book a meeting</h3><!-- /.appointment-one__title -->
            <p class="appointment-one__text">Book a free consultation with one of our experts...</p>
            <!-- /.appointment-one__text -->
            <form action="sendmail.php" method="POST" class="appointment-one__form row">
                <div class="col-lg-4">
                    <input type="text" class="miyami_input" placeholder="Name" name="name" required>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-4">
                    <input type="email" class="miyami_input" placeholder="Email" name="email" required>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-4">
                    <select name="discussion" class="selectpicker">
                        <option value="">Discussion about</option>
                        <option value="General">General</option>
                        <option value="IT Solutions">IT Solutions</option>
                        <option value="IT Staffing">IT Staffing</option>
                        <option value="IT Outsourcing">IT Outsourcing</option>
                    </select>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-12">
                    <div class="appointment-one__bottom">
                        <div class="form_button">
                            <button value="Book a meeting" class="miyami_btn g_bg" type="submit">Book a meeting</button>
                        </div>
                    </div><!-- /.appointment-one__bottom -->
                </div><!-- /.col-lg-12 -->
            </form><!-- /.appointment-one__form -->
        </div><!-- /.inner-container -->
    </div><!-- /.container -->
</section><!-- /.appointment-one -->

sendmail.php 文件

<?php
$errors = '';
$myemail = 'info@vitosolutions.co.za';//<-----Put Your email address here.
if(empty($_POST['name']) || empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$subjects = $_POST['subjects'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$discussion = $_POST['discussion'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail;
    $email_subject = "Email from VITO";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name".
    "\n Discussion: $discussion".
    "\n subject: $subjects \n ".
    "Email: $email_address\n Message: $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    $mail_status = mail($to,$email_subject,$email_body,$headers);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            //   alert('Thank you for the message. We will contact you shortly.');
            window.location.href = 'thankyou.html';
        </script>
        <?php
     }else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to info@vitosolutions.co.za');
            window.location.href = 'contact.html';
        </script>
    <?php }
//redirect to the 'thank you' page
header('Location: thankyou.html');
}
?>

但是当我在联系页面上提交表单时,它不会重定向。

不重定向的contact.html页面

<div class="contact-info-one__content">
    <div class="container contact-one">
        <div class="block-title text-center">
            <h2 class="block-title__title text-white">Get in touch</h2><!-- /.block-title__title -->
            <p class="block-title__text">Let us help you with your IT requirements.
            </p><!-- /.block-title__text -->
        </div><!-- /.block-title -->
        <form action="sendmail.php" method="POST" class="contact-form-vaidated contact-one__form row">
            <div class="col-lg-6">
                <input type="text" class="miyami_input" placeholder="Name" name="name" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-6">
                <input type="email" class="miyami_input" placeholder="Email" name="email" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-12">
                <input type="text" class="miyami_input" placeholder="Subject" name="subjects" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-12">
                <textarea class="miyami_textarea" placeholder="Message" name="message"></textarea>
            </div><!-- /.col-lg-12 -->
            <div class="col-lg-12 text-center">
                <div class="form_button">
                    <button value="Send" class="thm-btn" type="submit">Send</button>
                </div>
            </div><!-- /.col-lg-12 -->
        </form>

    </div><!-- /.container -->
</div><!-- /.contact-info-one__content -->

在提交表单之前 - 应该重定向

提交表单后它只是清除而不重定向

对 sendmail.php 的调用和响应

【问题讨论】:

  • 坚持使用header('Location: ......');进行重定向。 javascript 部分正在发送到浏览器,因此实际的header() 调用将不起作用,您应该收到headers already sent 错误
  • PS:language="javascript" 很久以前就被弃用了
  • 我尝试过使用重定向添加 if($mail_status){...} 我被卡住了,表单提交后页面没有重定向到感谢页面
  • 我已经删除了 javascript 部分。它仍然只在从 index.html 页面提交时重定向,而不是从 contact.html 页面提交时重定向

标签: php html


【解决方案1】:
<?php
$errors = '';
$myemail = 'info@vitosolutions.co.za';//<-----Put Your email address here.
if(empty($_POST['name']) || empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$subjects = $_POST['subjects'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$discussion = $_POST['discussion'];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email_address)) {
    $errors .= "\n Error: Invalid email address";
}

if(empty($errors)) {
    $to = $myemail;
    $email_subject = "Email from VITO";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name".
    "\n Discussion: $discussion".
    "\n subject: $subjects \n ".
    "Email: $email_address\n Message: $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    $mail_status = mail($to,$email_subject,$email_body,$headers);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            //   alert('Thank you for the message. We will contact you shortly.');
            window.location.href = 'thankyou.html';
        </script>
        <?php
    }else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to info@vitosolutions.co.za');
            window.location.href = 'contact.html';
        </script>
    <?php }
}
else {
    ?>
    <script language="javascript" type="text/javascript">
            alert('Errors: <?php echo $errors;?>');
        </script>
    <?php
}
?>

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习 - 您改变了什么,为什么?
【解决方案2】:

我总是在位置标头中包含重定向状态代码。我没有尝试过没有它,但我知道它确实可以使用它。

// status depends on http protocol
if(!$status) {
    $status = strpos($_SERVER['SERVER_PROTOCOL'],'1.1') ? 303 : 302;
}
// set redirect headers
http_response_code($status);
header("Location: $to");

在重定向之前发送的任何输出都将防止导致header() 失败,因此您可能需要先检查headers_sent()。这一直是我过去重定向问题的原因。

【讨论】:

猜你喜欢
  • 2020-05-27
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 2021-03-01
  • 2014-07-12
  • 2020-02-18
相关资源
最近更新 更多