PHP表单重定向到感谢页面

时间:2014-01-29 16:59:32

标签: php wordpress submit

我有一个自定义WP主题,我试图在表单验证后重定向到感谢页面。我知道有很多其他问题非常相似,但我已经尝试了“标题”技巧和所有其他建议,但我的页面只是继续回到contact.php页面。将鼠标悬停在提交按钮上(在点击之前)会显示mypageURL.com/contact,而不是mypageURL.com/thanks。这是我的代码。

<?php 
//Verify the email address
function isemail($email) {
    return preg_match('|^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$|i', $email);
}
//set variables
$error_name = false;
$error_email = false;
$error_message = false;
//Get form values
if (isset($_POST['contact-submit'])) {
    $contact_name = '';
    $contact_email = '';
    $contact_subject = '';
    $contact_message = '';
    $contact_reciever = '';

    if (trim($_POST['contact_name']) === '') {
        $error_name = true;
    } else {
        $contact_name = trim($_POST['contact_name']);
    }

    if (trim($_POST['contact_email']) === '' || !isemail(trim($_POST['contact_email']))) {
        $error_email = true;
    } else {
        $contact_email = trim($_POST['contact_email']);
    }

    $subject = trim($_POST['contact_subject']);

    if (trim($_POST['contact_message']) === '') {
        $error_message = true;
    } else {
        $contact_message = stripslashes(trim($_POST['contact_message']));
    }

    //Check for errors
    if (!$error_name && !$error_email && !$error_message) {
        //Get reciever email
        if( get_theme_mod( 'custom_contact_form_mail' ) != '') $get_contact_reciever = get_theme_mod( 'custom_contact_form_mail' ) ;
        $contact_reciever = $get_contact_reciever;

        $the_subject = 'New message: ' . $contact_subject;
        $the_message = 'Message from: ' . $contact_name . PHP_EOL . 'Email: ' . $contact_email . PHP_EOL . PHP_EOL . $contact_message . PHP_EOL ;

        $the_headers = "Form " . $contact_email . PHP_EOL . 'Reply-To: ' . $contact_email . PHP_EOL . 'MIME-Version: 1.0'  . PHP_EOL . 'Content-type: text/plain; charset=utf-8'  . PHP_EOL . 'Content-Transfer-Encoding: quoted-printable'  . PHP_EOL;

        if (mail($contact_reciever, $the_subject, $the_message, $the_headers)) {
            $contact_form_sent = true;
        } else {
            $contact_form_sent_error = true;
        }
    } else {
        $contact_form_not_filled = true;
    }
}

?>

<?php get_header(); ?>

<section id="content">

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

    <div class="white-section contact">
        <div class="container">
            <div class="row">
                <div class="span12">
                    <?php if (current_user_can('edit_post', $post->ID))
                        edit_post_link( $link = __('You are logged in as an Administrator. Click this text to edit this page. This text will not show up if you are not logged in as Admin.', 'cht'), $before = '<i class="icon-edit"></i> ', $after = '' );
                    ?>
                    <div class="row">
                        <div class="span6">
                            <?php the_content(); ?>
                            <h4><?php _e('Contact info', 'cht') ?></h4>
                            <ul>
                            <?php if( get_theme_mod( 'custom_contact_info_name' ) != '') { ?>
                                <li><i class="icon-briefcase"></i> <?php print get_theme_mod( 'custom_contact_info_name' ) ?></li>
                            <?php } else { ?>
                                <li><i class="icon-briefcase"></i> Cloud Hoster Ltd.</li>
                            <?php } ?>
                            <?php if( get_theme_mod( 'custom_contact_info_address' ) != '') { ?>
                                <li><i class="icon-map-marker"></i> <?php print get_theme_mod( 'custom_contact_info_address' ) ?></li>
                            <?php } else { ?>
                                <li><i class="icon-map-marker"></i> 01234 Main Street, New York 45678</li>
                            <?php } ?>
                            <?php if( get_theme_mod( 'custom_contact_info_phone' ) != '') { ?>
                                <li><i class="icon-phone"></i> <?php print get_theme_mod( 'custom_contact_info_phone' ) ?></li>
                            <?php } else { ?>
                                <li><i class="icon-phone"></i> Phone: 555-555-5555 Fax: 444-444-4444</li>
                            <?php } ?>
                            <?php if( get_theme_mod( 'custom_contact_info_mail' ) != '') { ?>
                                <li><i class="icon-envelope-alt"></i> Email: <a href="mailto:<?php print get_theme_mod( 'custom_contact_info_mail' ) ?>"><?php print get_theme_mod( 'custom_contact_info_mail' ) ?></a></li>
                            <?php } else { ?>
                                <li><i class="icon-envelope-alt"></i> Email: <a href="mailto:info@domain.com">info@domain.com</a></li>
                            <?php } ?>
                            </ul>
                        </div><!-- span6 end -->

                        <div class="span6">
                            <div id="map"></div>
                                <script>
                                jQuery(document).ready(function(){
                                    var map;
                                    map = new GMaps({
                                        div: '#map',
                                        <?php if( get_theme_mod( 'custom_google_map_lat' ) != '') { ?>
                                            lat: <?php print get_theme_mod( 'custom_google_map_lat' ) ?>,
                                        <?php } else { ?>
                                            lat: 40.714353,
                                        <?php } ?>
                                        <?php if( get_theme_mod( 'custom_google_map_lng' ) != '') { ?>
                                            lng: <?php print get_theme_mod( 'custom_google_map_lng' ) ?>,
                                        <?php } else { ?>
                                            lng: -74.005973,
                                        <?php } ?>
                                        zoom: 15,
                                        zoomControl: true,
                                        zoomControlOpt: {
                                            style : 'SMALL',
                                            position: 'TOP_LEFT'
                                        },
                                        streetViewControl: false,
                                    });

                                    map.addMarker({
                                        <?php if( get_theme_mod( 'custom_google_map_lat' ) != '') { ?>
                                            lat: <?php print get_theme_mod( 'custom_google_map_lat' ) ?>,
                                        <?php } else { ?>
                                            lat: 40.714353,
                                        <?php } ?>
                                        <?php if( get_theme_mod( 'custom_google_map_lng' ) != '') { ?>
                                            lng: <?php print get_theme_mod( 'custom_google_map_lng' ) ?>,
                                        <?php } else { ?>
                                            lng: -74.005973,
                                        <?php } ?>
                                    });
                                });
                                </script>
                        </div><!-- span6 end -->
                    </div><!-- row end -->

                    <div class="row">
                        <div class="span12">
                            <form action="<?php the_permalink(); ?>" method='post' name='contactform' id='contactform'>
                                <p><?php _e('Your name:', 'cht') ?></p>
                                <input type="text" class="input-box" name="contact_name" value="<?php if (isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" placeholder="<?php _e('Please enter your name.', 'cht') ?>">
                                <p><?php _e('Email address:', 'cht') ?></p>
                                <input type="text" class="input-box" name="contact_email" value="<?php if (isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" placeholder="<?php _e('Please enter your email address.', 'cht') ?>">
                                <p><?php _e('What kind of problems are you having?', 'cht') ?></p>
                                <input type="text" class="input-box" name="contact_subject" value="<?php if (isset($_POST['contact_subject'])) echo $_POST['contact_subject']; ?>" placeholder="<?php _e('Purpose of this message.', 'cht') ?>">
                                <p class="right-message-box"><?php _e('How Can We Help You?', 'cht') ?></p>
                                <textarea class="input-box right-message-box message-box" name="contact_message" value="<?php if (isset($_POST['contact_message'])) echo stripslashes($_POST['contact_message']); ?>" placeholder="<?php _e('Your message.', 'cht') ?>"></textarea>
                                <button type='submit' class='submit-contact-form' name='submit' id="submit">Send your message</button>
                                <input type="hidden" name="contact-submit" id="contact-submit" value="true">
                            </form>
                        </div><!-- span12 end -->                   
                    </div><!-- row end -->

                        <?php if (isset($contact_form_sent) && $contact_form_sent == true) : ?>
                            <div class="alert alert-success"><p><strong><?php _e('Success! ', 'cht') ?> </strong><?php _e('Your message has been sent.', 'cht') ?></p></div>
                        <?php elseif (isset($contact_form_sent_error) && $contact_form_sent_error == true) : ?>
                            <div class="alert alert-error"><p><strong><?php _e('Error! ', 'cht') ?> </strong><?php _e('Something went wrong. Please try again.', 'cht') ?></p></div>
                        <?php elseif (isset($contact_form_not_filled) && $contact_form_not_filled == true) : ?>
                            <div class="alert alert-error"><p><strong><?php _e('Error! ', 'cht') ?> </strong><?php _e('Fill out the form correctly and try again.', 'cht') ?></p></div>
                        <?php endif; ?>

                </div><!-- span12 end -->
            </div><!-- row end -->
        </div><!-- conteiner end -->
    </div><!-- white-section end -->

    <?php endwhile; endif; ?>

</section><!-- content end -->

<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

这不是一个真正的技巧,它是如何完成你想做的事情。

if (mail($contact_reciever, $the_subject, $the_message, $the_headers)) {
    $contact_form_sent = true;
    header("Location: " . get_permalink($THANKYOU_PAGE_ID));
}

尝试使用标头时是否会丢失任何错误?如果是这样,您可能必须创建一个钩子并在页面加载中更早地验证该表单。

我假设您的评论意味着“是的,它会丢失错误,我如何更早地挂钩我的表单,以便它不会那样做?”。好吧,我的好朋友跟着我......

add_action( 'send_headers', 'form_verify' );
function form_verify() {
    // add form code here with header code
}