2

I am fairly new to PHP and am working on a word press site. The current contact form throws up message sent banner when submitted. However, I would like to redirect to a thank you page I have built. The coding is below. Please help. I have looked at the other similar questions and still can't figure it out.

<?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 ========== -->
<!-- ================================ -->
N.Akin
  • 43
  • 5
  • Possible duplicate of [How to make a redirect in PHP?](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – David Feb 10 '16 at 19:52

1 Answers1

0

You can use a php header() redirection:

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

In your code:

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

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
Source: PHP header()


Or you can use javascript with timeout function so your "message sent banner" will be visible some seconds before redirection occurs;

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

In your code:

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

Source: StackOverflow answer


Try this code:

<?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 ========== -->
<!-- ================================ -->
Community
  • 1
  • 1
Math
  • 666
  • 8
  • 26
  • So were would I add the PHP header? – N.Akin Feb 10 '16 at 20:08
  • I updated my answer. You have to use header() just after `$mail()->send` – Math Feb 10 '16 at 20:10
  • it kicked back the following error: Warning: Cannot modify header information - headers already sent by (output started at /home/pacificcancerins/public_html/wp-content/themes/medic-final/page-contact.php:20) in /home/pacificcancerins/public_html/wp-content/themes/medic-final/page-contact.php on line 104 – N.Akin Feb 10 '16 at 20:14
  • So you cannot use header() because header() as i stated before must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. Try the second solution. – Math Feb 10 '16 at 20:16
  • What would be the best way of doing that? Thanks again for your help! – N.Akin Feb 10 '16 at 20:20
  • My bad I just saw where you made the in my code edit for the java. I made the change and it works perfectly! thank you so much. – N.Akin Feb 10 '16 at 20:25
  • If it answer your question, consider to accept my answer :) And dont hesitate to look at ThomasB's answer, his "keep it simple" solution is worth a try ! Wordpress works with plugins etc, and is not meant to be edited in core. Because a Wordpress update would possibly reset all your "hunder the hood" scripting – Math Feb 10 '16 at 20:31