PHP联系表单成功发送电子邮件但无法重定向到感谢页面

时间:2016-07-27 04:25:07

标签: php forms redirect contact-form

我是PHP的初学者。

这是一个联系我们'用户可以发送问题的表单。 目前,它已成功将提交的问题直接发送到我们的电子邮箱。但是,它在提交后无法重定向到感谢页面。 我真诚地希望有人能帮助我。我输了。代码如下。提前谢谢。

<?php

// Define some constants
define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );

// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, $subject, $message, $headers );
}

if ( mail( $recipient, $subject, $message, $headers )) {
    header ("Location: thanks.html");
    exit ();
}

?>

---- ---- EDIT

这是HTML:

<section class="contact_us_container">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align:center;"> <!-- section title -->
                        <h2>Get In Touch With Us</h2>
                        <p>Type your question here and we will get back to you as soon as possible.</p>
                    </div> <!-- End section title -->
                    <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 form_holder"> <!-- form holder -->
                        <form action="includes/sendemail.php" class="contact-form">
                            <input class="form-control name" type="text" name="name" placeholder="Your Name">
                            <input class="form-control email" type="email" name="email" placeholder="Your Email">
                            <input class="form-control" type="text" name="subject" placeholder="Subject">
                            <textarea name="message" placeholder="Your Message"></textarea>
                            <button type="submit" class="submit">submit now<i class="fa fa-arrow-circle-right"></i></button>
                        </form> <!-- End form holder -->
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pull-right address">
                        <address>
                            <div class="icon_holder float_left"><span class="icon icon-Pointer"></span></div>
                            <div class="address_holder float_left">M2-19-03,<br>8trium Tower, Jalan Cempaka SD 12/5,<br>Bandar Sri Damansara,52000 KL</div>
                        </address>
                        <address class="clear_fix">
                            <div class="icon_holder float_left"><span class="icon icon-Plaine"></span></div>
                            <div class="address_holder float_left">enquiry@choicebaby.com.my</div>
                        </address>
                        <address class="clear_fix">
                            <div class="icon_holder float_left"><span class="icon icon-Phone2"></span></div>
                            <div class="address_holder float_left">+ 603 6735 5374</div>
                        </address>
                    </div>
                </div>
            </div>
        </section>

4 个答案:

答案 0 :(得分:0)

HTML代码:

<form action="includes/sendemail.php" class="contact-form" method="post">
    <input class="form-control name" type="text" name="name" placeholder="Your Name">
    <input class="form-control email" type="email" name="email" placeholder="Your Email">
    <input class="form-control" type="text" name="subject" placeholder="Subject">
    <textarea name="message" placeholder="Your Message"></textarea>
    <button type="submit" class="submit" name="btnSubmit">submit now<i class="fa fa-arrow-circle-right"></i></button>
</form>

PHP代码:includes / sendemail.php

<?php
if(isset($_POST["btnSubmit"]))
{
    // Define some constants
    define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
    define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );

    // Read the form values
    $success = false;
    $senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
    $senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
    $subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
    $message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

    // If all values exist, send the email
    if ( $senderName && $senderEmail && $message ) {
      $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
      $headers = "From: " . $senderName . " <" . $senderEmail . ">";
      $success = mail( $recipient, $subject, $message, $headers );
    }

    if ( mail( $recipient, $subject, $message, $headers ))
    {
        header ("Location: thanks.html");
        exit ();
    }
}
?>

替换为此代码..

答案 1 :(得分:0)

我会像这样使用jquery / ajax:

我想你的文件中有你的表格

<!DOCTYPE html>

<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content=""> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="functions.js"></script>
</head>

 <body>

    <form  role="form" method="post" id="subscribe">
       <input type="text" name="name" id="name" value="" placeholder="Name"/>
       <input type="email" name="email" id="email" value="" placeholder="Email"/>
       <input type="text" name="subject" id="subject" value="" placeholder="Subject"/>
       <textarea rows="4" cols="50" name="message" id="message"> </textarea>
       <input type="submit" class="button" name="submit" value="Submit">
    </form>
    <div class="message"></div>
    <div class="message-notice"></div>

 </body>
 </html>

您的PHP文件,其中有您的PHP脚本。(假设它被称为subscribe.php)

<?php

// Define some constants
define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );

// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, $subject, $message, $headers );
}

if ( mail( $recipient, $subject, $message, $headers )) {
    //header ("Location: thanks.html");
    echo "success";
    exit ();
}else{
    echo "fail";
}

?>

你缺少的是这个javascript脚本:

;(function($, window, document, undefined) {
var $win = $(window);
var $doc = $(document);

$doc.ready(function() {

    $('#subscribe').submit(function() {
        $.ajax({
            url: 'subscribe.php', 
            data: $('#subscribe').serialize(),
            type: 'POST',
            success: function(msg) {
                if(msg=="success"){
                    $("#subscribe").hide(); //Hide your form
                    $(".message").show(); //Show the message
                    $(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
                    window.setTimeout(function(){
                        window.location.href = "http://www.yoururl.com/thanks.html"; 
                    }, 10000); //After 10seconds you will be redirect to the thanks page.
                    document.getElementById("subscribe").reset();
                } else {
                    $(".message-notice").fadeIn("slow");
                    $(".message-notice").html(msg);
                }
            }
        });
        return false;
    });
});

})(jQuery, window, document);

希望它有所帮助。

答案 2 :(得分:0)

您是否尝试将ob_start()放在代码的开头?

或者,JavaScript的document.location怎么样?

答案 3 :(得分:0)

plus method =&#34; post&#34;在表单标签

<form action="includes/sendemail.php" class="contact-form" method="post">