重定向联系表单以感谢您页面

时间:2016-02-10 19:48:28

标签: php wordpress

我是PHP的新手,正在开发一个word新闻网站。当前联系表单在提交时会抛出消息发送横幅。但是,我想重定向到我建立的感谢页面。编码如下。请帮忙。我看过其他类似的问题,仍然无法弄明白。

<?php
/*
 * Template Name: Contact Page
 */

// File Security Check
if ( ! defined( 'ABSPATH' ) ) { exit; }


require_once('assets/mailer/PHPMailerAutoload.php');

wp_enqueue_script('googleMap', THEMEROOT . '/assets/js/map.js', array(), '1.0', true);

$prefix = Haze_Meta_Boxes::get_instance()->prefix;
$email_address = rwmb_meta("{$prefix}contact_email");
$contact_address = rwmb_meta("{$prefix}contact_address");

?>

<?php

$error_name_app = false;
$error_email_app = false;
$error_message_app = false;
$error_subject_app = false;
$error_select_app = false;


if (isset($_POST['contact-submit'])) {

    // Initialize the variables
$name_app = '';
$email_app = '';
$message_app = '';
$subject_app = '';
$select_app = '';
$receiver_email_app = '';

// Get the name
if (trim($_POST['firstName']) === '') {
    $error_name_app = true;
} else {
    $name_app = trim($_POST['firstName']);
}

// Get the email
if (trim($_POST['email']) === '' || !isEmailwidget($_POST['email'])) {
    $error_email_app = true;
} else {
    $email_app = trim($_POST['email']);
}

// Get the message
if (trim($_POST['comments']) === '') {
    $error_message_app = true;
} else {
    $message_app = stripslashes(trim($_POST['comments']));
}

// Get the Subject
if (trim($_POST['subject']) === '') {
    $error_subject_app = true;
} else {
    $subject_app = stripslashes(trim($_POST['subject']));
}

// Get the Referral
if (trim($_POST['referral']) === '') {
    $error_select_app = true;
} else {
    $select_app = stripslashes(trim($_POST['referral']));
}

// Check if we have errors
if (!$error_name_app && !$error_email_app) {

    $receiver_email_app = $email_address;

    // If none is specified, get the WP admin email
    if (!isset($receiver_email_app) || $receiver_email_app == '') {
        $receiver_email_app = get_option('admin_email');
    }

    $phone_app = trim($_POST['phone']);

    $mail = new PHPMailer;

    // Construct the email
    $mail->From = $receiver_email_app;
    $mail->FromName = get_bloginfo('name');
    $mail->addAddress($receiver_email_app);
    $mail->addReplyTo($email_app, $name_app);
    $mail->isHTML(false);

    $mail->Subject = __('New Message from ', LANGUAGE_ZONE) . $name_app;

    $mail->Body    = 'Name: ' . $name_app;
    $mail->Body    .= PHP_EOL . 'Tel: ' . $phone_app;
    $mail->Body    .= PHP_EOL . 'How can we help: ' . $subject_app;
    $mail->Body    .= PHP_EOL . 'How did you hear about us: ' . $select_app;
    $mail->Body    .= PHP_EOL . 'Message: ' . $message_app;

    if ($mail->send()) {
        $email_sent_app = true;
    } else {
        $email_sent_error_app = true;
    }

}
}
?>

<!-- ================================================== -->
<!-- =============== START HEADER ================ -->
<!-- ================================================== -->
<?php get_header(); ?>
<!-- ================================================== -->
<!-- =============== END HEADER ================ -->
<!-- ================================================== -->


<!-- ================================ -->
<!-- ========== Begin Big Map ========== -->
<!-- ================================ -->
<section class="big_map">
    <div class="container xsx-width">
        <div class="row">
            <div class="col-md-12">
                <?php if($contact_address != '') {
                    echo clx_get_google_maps($contact_address, true, 350);
                } ?>
            </div>
        </div>
    </div>
</section>
<!-- ================================ -->
<!-- ========== Begin Big Map ========== -->
<!-- ================================ -->

<!-- ================================ -->
<!-- ========== Begin Appointment ========== -->
<!-- ================================ -->
<section class="appointments">
    <div class="container xsx-width">
        <div class="row">
            <!-- ===== Begin Contact Form ===== -->
            <div class="col-sm-14 appointment">

                <!-- ===== Begin Title Content ===== -->
                <div class="titleC">
                    <h4><?php _e('CONTACT FORM', LANGUAGE_ZONE_ADMIN); ?></h4>
                    <div class="right-linie">
                        <span></span>
                    </div>
                </div>
                <!-- ===== End Title Content ===== -->

                <?php if ( have_posts() ) : ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                        <?php the_content(); ?>

                        <?php if(isset($email_sent_app) && $email_sent_app == true): ;?>
                            <div class="alert alert-success fade in">
                                <div class="icon-alert"></div>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                                <?php _e('Message Successfully Sent!', LANGUAGE_ZONE); ?>
                            </div>
                        <?php elseif(isset($email_sent_error_app) && $email_sent_error_app == true): ?>

                            <div class="alert alert-warning fade in">
                                <div class="icon-alert"></div>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                                <?php _e('Please check if you have filled all the fields with valid information and try again. Thank you.', LANGUAGE_ZONE); ?>
                            </div>

                            <form action="<?php the_permalink(); ?>" method="post" class="comment-form">
                                <ul class="comment-form-inputs">
                                  <li>
                                        <input id="firstName" name="firstName" type="text" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>" aria-required="true" placeholder="<?php _e('Name*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="email" name="email" type="text" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" aria-required="true" placeholder="<?php _e('Email*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="phone" name="phone" type="text" value="<?php if(isset($_POST['phone'])) echo $_POST['phone']; ?>" placeholder="<?php _e('Phone', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                               <li>      
                                <label for="subject" name="label" id="label">How can we help you?
                <select name="subject" id="subject" required>
                                            <option value="Select">Select An Option</option>
                                            <option value="Request Additional Information">Request Additional Information</option>
                    <option value="Scheduling Question">Scheduling Question</option>
                    <option value="Clinical Question">Clinical Question</option>
                    <option value="Billing Question">Billing Question</option>
                    <option value="Compliment">Compliment</option>
                    <option value="Complaint">Complaint</option>
                    <option value="Other">Other</option>
                    </option>
                </select></label>
                                  </li>                                       
                                  <li>      
                                <label for="referral" name="label2" id="label2">How did you hear about Pacific Cancer Institute?
                <select name="referral" id="referral" required>
                                            <option value="Select">Select An Option</option>                        
                                            <option value="TV">TV</option>
                    <option value="Radio">Radio</option>
                    <option value="Billboard">Billboard</option>
                    <option value="Internet">Internet</option>
                    <option value="Friend">Friend</option>
                    <option value="Physician">Physician</option>
                </select></label>
                                  </li>
                                </ul>
                                <textarea name="comments" rows="10" placeholder="<?php _e('We welcome your general comments, questions, and suggestions. This email form is for general clinic information ONLY. To discuss any medical symptoms or conditions, you should contact us directly at the phone number listed. By submitting this form, you agree to our terms and conditions and allow this information to be viewed by our patient coordinator and necessary clinical personnel.', LANGUAGE_ZONE_ADMIN); ?>"><?php if(isset($_POST['comments'])) echo stripslashes($_POST['comments']); ?></textarea>

                                <input type="hidden" name="contact-submit" id="contact-submit" value="true" />

                                <p class="form-submit">
                                    <input name="submit" id="submit" type="submit" value="<?php _e('Send', LANGUAGE_ZONE); ?>">
                                </p>
                            </form>

                        <?php else : ?>

                            <form action="<?php the_permalink(); ?>" method="post" class="comment-form">
                                <ul class="comment-form-inputs">
                                  <li>
                                        <input id="firstName" name="firstName" type="text" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>" aria-required="true" placeholder="<?php _e('Name*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="email" name="email" type="text" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" aria-required="true" placeholder="<?php _e('Email*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="phone" name="phone" type="text" value="<?php if(isset($_POST['phone'])) echo $_POST['phone']; ?>" placeholder="<?php _e('Phone', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                               <li>      
                                <label for="subject" name="label" id="label">How can we help you?
                <select name="subject" id="subject" required>
                                            <option value="Select">Select An Option</option>
                                            <option value="Request Additional Information">Request Additional Information</option>
                    <option value="Scheduling Question">Scheduling Question</option>
                    <option value="Clinical Question">Clinical Question</option>
                    <option value="Billing Question">Billing Question</option>
                    <option value="Compliment">Compliment</option>
                    <option value="Complaint">Complaint</option>
                    <option value="Other">Other</option>
                    </option>
                </select></label>
                                  </li>                                       
                                  <li>      
                                <label for="referral" name="label2" id="label2">How did you hear about Pacific Cancer Institute?
                <select name="referral" id="referral" required>
                                            <option value="Select">Select An Option</option>                        
                                            <option value="TV">TV</option>
                    <option value="Radio">Radio</option>
                    <option value="Billboard">Billboard</option>
                    <option value="Internet">Internet</option>
                    <option value="Friend">Friend</option>
                    <option value="Physician">Physician</option>
                </select></label>
                                  </li>
                                </ul>
                                <textarea name="comments" rows="10" placeholder="<?php _e('We welcome your general comments, questions, and suggestions. This email form is for general clinic information ONLY.  To discuss any medical symptoms or conditions, you should contact us directly at the phone number listed. By submitting this form, you agree to our terms and conditions and allow this information to be viewed by our patient coordinator and necessary clinical personnel.', LANGUAGE_ZONE_ADMIN); ?>"><?php if(isset($_POST['comments'])) echo stripslashes($_POST['comments']); ?></textarea>

                                <input type="hidden" name="contact-submit" id="contact-submit" value="true" />

                                <p class="form-submit">
                                    <input name="submit" id="submit" type="submit" value="<?php _e('Send', LANGUAGE_ZONE); ?>">
                                </p>
                            </form>

                        <?php endif; ?>


                    <?php endwhile; ?>

                     <?php else : ?>

                    <?php
                    /* Get the none-content template (error) */
                    get_template_part( 'content', 'none' );
                    ?>

                <?php endif; ?>

            </div>
            <!-- ===== End Contact Form ===== -->

        </div>
    </div>
</section>
<!-- ================================ -->
<!-- ========== End Appointment ========== -->
<!-- ================================ -->

1 个答案:

答案 0 :(得分:0)

您可以使用php header()重定向:

header('Location: http://www.example.com/');
exit;

在您的代码中:

if ($mail->send()) {
    header('Location: http://www.yourwebsite.com/thankspage.php');
    exit;
} else {
    $email_sent_error_app = true;
}
必须在发送任何实际输出之前调用

header(),无论是普通HTML标记,文件中的空行还是PHP。 资料来源:PHP header()

或者您可以使用具有超时功能的javascript,以便您的&#34;消息发送横幅&#34;将在重定向发生前几秒钟显示;

<script>
    window.setTimeout(function(){ window.location = "http://www.yoururl.com"; },3000);
</script> 

在您的代码中:

if ($mail->send()) {
    echo "<script>";
    echo "window.setTimeout(function(){ window.location = \"http://www.yoururl.com\"; },3000);";
    echo "</script>";
} else {
    $email_sent_error_app = true;
}

来源:StackOverflow answer

试试这段代码:

<?php
/*
 * Template Name: Contact Page
 */

// File Security Check
if ( ! defined( 'ABSPATH' ) ) { exit; }


require_once('assets/mailer/PHPMailerAutoload.php');

wp_enqueue_script('googleMap', THEMEROOT . '/assets/js/map.js', array(), '1.0', true);

$prefix = Haze_Meta_Boxes::get_instance()->prefix;
$email_address = rwmb_meta("{$prefix}contact_email");
$contact_address = rwmb_meta("{$prefix}contact_address");

$error_name_app = false;
$error_email_app = false;
$error_message_app = false;
$error_subject_app = false;
$error_select_app = false;


if (isset($_POST['contact-submit'])) {

    // Initialize the variables
$name_app = '';
$email_app = '';
$message_app = '';
$subject_app = '';
$select_app = '';
$receiver_email_app = '';

// Get the name
if (trim($_POST['firstName']) === '') {
    $error_name_app = true;
} else {
    $name_app = trim($_POST['firstName']);
}

// Get the email
if (trim($_POST['email']) === '' || !isEmailwidget($_POST['email'])) {
    $error_email_app = true;
} else {
    $email_app = trim($_POST['email']);
}

// Get the message
if (trim($_POST['comments']) === '') {
    $error_message_app = true;
} else {
    $message_app = stripslashes(trim($_POST['comments']));
}

// Get the Subject
if (trim($_POST['subject']) === '') {
    $error_subject_app = true;
} else {
    $subject_app = stripslashes(trim($_POST['subject']));
}

// Get the Referral
if (trim($_POST['referral']) === '') {
    $error_select_app = true;
} else {
    $select_app = stripslashes(trim($_POST['referral']));
}

// Check if we have errors
if (!$error_name_app && !$error_email_app) {

    $receiver_email_app = $email_address;

    // If none is specified, get the WP admin email
    if (!isset($receiver_email_app) || $receiver_email_app == '') {
        $receiver_email_app = get_option('admin_email');
    }

    $phone_app = trim($_POST['phone']);

    $mail = new PHPMailer;

    // Construct the email
    $mail->From = $receiver_email_app;
    $mail->FromName = get_bloginfo('name');
    $mail->addAddress($receiver_email_app);
    $mail->addReplyTo($email_app, $name_app);
    $mail->isHTML(false);

    $mail->Subject = __('New Message from ', LANGUAGE_ZONE) . $name_app;

    $mail->Body    = 'Name: ' . $name_app;
    $mail->Body    .= PHP_EOL . 'Tel: ' . $phone_app;
    $mail->Body    .= PHP_EOL . 'How can we help: ' . $subject_app;
    $mail->Body    .= PHP_EOL . 'How did you hear about us: ' . $select_app;
    $mail->Body    .= PHP_EOL . 'Message: ' . $message_app;

    if ($mail->send()) {
        header('Location: http://www.yourwebsite.com/thankspage.php');
        exit;
    } else {
        $email_sent_error_app = true;
    }

}
}
?>

<!-- ================================================== -->
<!-- =============== START HEADER ================ -->
<!-- ================================================== -->
<?php get_header(); ?>
<!-- ================================================== -->
<!-- =============== END HEADER ================ -->
<!-- ================================================== -->


<!-- ================================ -->
<!-- ========== Begin Big Map ========== -->
<!-- ================================ -->
<section class="big_map">
    <div class="container xsx-width">
        <div class="row">
            <div class="col-md-12">
                <?php if($contact_address != '') {
                    echo clx_get_google_maps($contact_address, true, 350);
                } ?>
            </div>
        </div>
    </div>
</section>
<!-- ================================ -->
<!-- ========== Begin Big Map ========== -->
<!-- ================================ -->

<!-- ================================ -->
<!-- ========== Begin Appointment ========== -->
<!-- ================================ -->
<section class="appointments">
    <div class="container xsx-width">
        <div class="row">
            <!-- ===== Begin Contact Form ===== -->
            <div class="col-sm-14 appointment">

                <!-- ===== Begin Title Content ===== -->
                <div class="titleC">
                    <h4><?php _e('CONTACT FORM', LANGUAGE_ZONE_ADMIN); ?></h4>
                    <div class="right-linie">
                        <span></span>
                    </div>
                </div>
                <!-- ===== End Title Content ===== -->

                <?php if ( have_posts() ) : ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                        <?php the_content(); ?>

                        <?php if(isset($email_sent_app) && $email_sent_app == true): ;?>
                            <div class="alert alert-success fade in">
                                <div class="icon-alert"></div>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                                <?php _e('Message Successfully Sent!', LANGUAGE_ZONE); ?>
                            </div>
                        <?php elseif(isset($email_sent_error_app) && $email_sent_error_app == true): ?>

                            <div class="alert alert-warning fade in">
                                <div class="icon-alert"></div>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                                <?php _e('Please check if you have filled all the fields with valid information and try again. Thank you.', LANGUAGE_ZONE); ?>
                            </div>

                            <form action="<?php the_permalink(); ?>" method="post" class="comment-form">
                                <ul class="comment-form-inputs">
                                  <li>
                                        <input id="firstName" name="firstName" type="text" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>" aria-required="true" placeholder="<?php _e('Name*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="email" name="email" type="text" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" aria-required="true" placeholder="<?php _e('Email*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="phone" name="phone" type="text" value="<?php if(isset($_POST['phone'])) echo $_POST['phone']; ?>" placeholder="<?php _e('Phone', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                               <li>      
                                <label for="subject" name="label" id="label">How can we help you?
                <select name="subject" id="subject" required>
                                            <option value="Select">Select An Option</option>
                                            <option value="Request Additional Information">Request Additional Information</option>
                    <option value="Scheduling Question">Scheduling Question</option>
                    <option value="Clinical Question">Clinical Question</option>
                    <option value="Billing Question">Billing Question</option>
                    <option value="Compliment">Compliment</option>
                    <option value="Complaint">Complaint</option>
                    <option value="Other">Other</option>
                    </option>
                </select></label>
                                  </li>                                       
                                  <li>      
                                <label for="referral" name="label2" id="label2">How did you hear about Pacific Cancer Institute?
                <select name="referral" id="referral" required>
                                            <option value="Select">Select An Option</option>                        
                                            <option value="TV">TV</option>
                    <option value="Radio">Radio</option>
                    <option value="Billboard">Billboard</option>
                    <option value="Internet">Internet</option>
                    <option value="Friend">Friend</option>
                    <option value="Physician">Physician</option>
                </select></label>
                                  </li>
                                </ul>
                                <textarea name="comments" rows="10" placeholder="<?php _e('We welcome your general comments, questions, and suggestions. This email form is for general clinic information ONLY. To discuss any medical symptoms or conditions, you should contact us directly at the phone number listed. By submitting this form, you agree to our terms and conditions and allow this information to be viewed by our patient coordinator and necessary clinical personnel.', LANGUAGE_ZONE_ADMIN); ?>"><?php if(isset($_POST['comments'])) echo stripslashes($_POST['comments']); ?></textarea>

                                <input type="hidden" name="contact-submit" id="contact-submit" value="true" />

                                <p class="form-submit">
                                    <input name="submit" id="submit" type="submit" value="<?php _e('Send', LANGUAGE_ZONE); ?>">
                                </p>
                            </form>

                        <?php else : ?>

                            <form action="<?php the_permalink(); ?>" method="post" class="comment-form">
                                <ul class="comment-form-inputs">
                                  <li>
                                        <input id="firstName" name="firstName" type="text" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>" aria-required="true" placeholder="<?php _e('Name*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="email" name="email" type="text" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" aria-required="true" placeholder="<?php _e('Email*', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                                    <li>
                                        <input id="phone" name="phone" type="text" value="<?php if(isset($_POST['phone'])) echo $_POST['phone']; ?>" placeholder="<?php _e('Phone', LANGUAGE_ZONE_ADMIN); ?>">
                                    </li>
                               <li>      
                                <label for="subject" name="label" id="label">How can we help you?
                <select name="subject" id="subject" required>
                                            <option value="Select">Select An Option</option>
                                            <option value="Request Additional Information">Request Additional Information</option>
                    <option value="Scheduling Question">Scheduling Question</option>
                    <option value="Clinical Question">Clinical Question</option>
                    <option value="Billing Question">Billing Question</option>
                    <option value="Compliment">Compliment</option>
                    <option value="Complaint">Complaint</option>
                    <option value="Other">Other</option>
                    </option>
                </select></label>
                                  </li>                                       
                                  <li>      
                                <label for="referral" name="label2" id="label2">How did you hear about Pacific Cancer Institute?
                <select name="referral" id="referral" required>
                                            <option value="Select">Select An Option</option>                        
                                            <option value="TV">TV</option>
                    <option value="Radio">Radio</option>
                    <option value="Billboard">Billboard</option>
                    <option value="Internet">Internet</option>
                    <option value="Friend">Friend</option>
                    <option value="Physician">Physician</option>
                </select></label>
                                  </li>
                                </ul>
                                <textarea name="comments" rows="10" placeholder="<?php _e('We welcome your general comments, questions, and suggestions. This email form is for general clinic information ONLY.  To discuss any medical symptoms or conditions, you should contact us directly at the phone number listed. By submitting this form, you agree to our terms and conditions and allow this information to be viewed by our patient coordinator and necessary clinical personnel.', LANGUAGE_ZONE_ADMIN); ?>"><?php if(isset($_POST['comments'])) echo stripslashes($_POST['comments']); ?></textarea>

                                <input type="hidden" name="contact-submit" id="contact-submit" value="true" />

                                <p class="form-submit">
                                    <input name="submit" id="submit" type="submit" value="<?php _e('Send', LANGUAGE_ZONE); ?>">
                                </p>
                            </form>

                        <?php endif; ?>


                    <?php endwhile; ?>

                     <?php else : ?>

                    <?php
                    /* Get the none-content template (error) */
                    get_template_part( 'content', 'none' );
                    ?>

                <?php endif; ?>

            </div>
            <!-- ===== End Contact Form ===== -->

        </div>
    </div>
</section>
<!-- ================================ -->
<!-- ========== End Appointment ========== -->
<!-- ================================ -->