如何将联系表单重定向到感谢页面

时间:2012-11-22 16:30:13

标签: php forms wordpress redirect contact

我正在使用高级Wordpress主题,我正在努力获取主题附带的自定义联系表单,以便在成功完成后重定向到感谢页面(这是用于转换跟踪目的)

无论如何,这里是在页面上输入时调用短代码的代码

    add_shortcode('etheme_contacts', 'etheme_contacts_shortcodes');
    function etheme_contacts_shortcodes($atts, $content=null){
    $a = shortcode_atts( array(
       'gmap' => 1
    ), $atts );
    if(isset($_GET['contactSubmit'])){
    $emailFrom = strip_tags($_GET['contactEmail']);
    $emailTo = etheme_get_option('contacts_email');
    $subject = strip_tags($_GET['contactSubject']);

    $name = strip_tags($_GET['contactName']); 
    $email = strip_tags($_GET['contactEmail']); 
    $message = strip_tags(stripslashes($_GET['contactMessage'])); 

    $body = "Name: ".$name."\n";
    $body .= "Email: ".$email."\n";
    $body .= "Message: ".$message."\n";
    $body .= $name.", <b>".$emailFrom."</b>\n";

    $headers = "From: ".$emailFrom."\n";
    $headers .= "Reply-To:".$emailFrom."\n";    

    if(isset($_GET['contactSubmit'])){
        $success = mail($emailTo, $subject, $body, $headers);
        if ($success){
        echo '<p class="yay">All is well, your e&ndash;mail has been sent.</p>';
        } 
    } else {
        echo '<p class="oops">Something went wrong</p>';
    }
    } else {
    if($a['gmap'] == 1):
    ?>

    <div id="map">
        <p>Enable your JavaScript!</p>
    </div>
    <script type="text/javascript">
        var $map = jQuery('#map');    
        if( $map.length ) {    
            $map.gMap({
                address: '<?php etheme_option('google_map'); ?>',
                zoom: 16,
                markers: [
                    { 'address' : '<?php etheme_option('google_map'); ?>' }
                ]
            });    
        }  
        var succmsg = '<?php _e('All is well, your e&ndash;mail has been sent!',     ETHEME_DOMAIN); ?>';
    </script>
    <?php endif; ?>
    <?php if(etheme_option('contacts_custom_html') != ''): ?>
    <div class="custom-html">
        <?php echo etheme_option('contacts_custom_html') ?>
    </div>
    <?php endif; ?>
    <div class="one-third">      
        <div id="contactsMsgs"></div>  
        <form action="<?php the_permalink(); ?>" method="POST" class="form"      id="ethemeContactForm">   
            <div class="formField">
                <label for="contactName"><?php _e('Name', ETHEME_DOMAIN); ?> <span class="required">*</span></label>
                <input type="text" class="textField required-field" name="contactName" id="contactName" />
                <div class="clear"></div>
            </div>
            <div class="formField">
                <label for="contactEmail"><?php _e('Email', ETHEME_DOMAIN); ?> <span class="required">*</span></label>
                <input type="text" class="textField required-field email" name="contactEmail" id="contactEmail" />
                <div class="clear"></div>
            </div>
            <div class="formField">
                <label for="contactSubject"><?php _e('Subject', ETHEME_DOMAIN); ?></label>
                <input type="text" class="textField" name="contactSubject" id="contactSubject" />
                <div class="clear"></div>
            </div>
            <div class="formField">
                <label for="contactMessage"><?php _e('Message', ETHEME_DOMAIN); ?> <span class="required">*</span></label>
                <textarea class="textField required-field" name="contactMessage" id="contactMessage" cols="30" rows="10"></textarea>
                <div class="clear"></div>
            </div>
            <div class="formField ">
                <button class="button" name="contactSubmit" type="submit"><span><?php _e('Send Request', ETHEME_DOMAIN); ?></span></button>
                <div class="contactSpinner"></div>
            </div>
        </form>      
    </div>
    <div class="one-third last fl-r">
        <div class="block non-line contats">
            <?php etheme_option('contacts_info'); ?>
        </div>
    </div>
<?php
}
}

我假设有一些东西需要在这里更改,因为这是php为正在提交的表单设置函数的地方,但由于我的noobness,我不知道如何处理这段代码。

if(isset($_GET['contactSubmit'])){
$success = mail($emailTo, $subject, $body, $headers);
if ($success){
echo '<p class="yay">All is well, your e&ndash;mail has been sent.</p>';
} 
} else {
echo '<p class="oops">Something went wrong</p>';
}

4 个答案:

答案 0 :(得分:5)

试试这个:

header( 'Location: http://www.yoursite.com/thanyk_you.html' ) ;

替换当前:

echo '<p class="yay">All is well, your mail has been sent.</p>'; 

答案 1 :(得分:3)

只需将所需的网址添加到“其他设置”框中

即可
  

on_sent_ok:“location ='http://example.com/';”

请记住,你应该在footer.php中包含页脚功能,否则它将无效。

第二个解决方案是更改插件文件中的编码。

希望它会对你有所帮助。

答案 2 :(得分:2)

这里有几个解决方案,但打字量最少的解决方案如下:

  1. 制作一个感谢页面(在wordpress中)并抓住网址
  2. 使用您的网址替换带有<meta http-equiv='refresh' content='0; url=http://stackoverflow.com'>header( 'Location: http://www.yoursite.com/new_page.html' ) ;的成功回显线,而不是使用stackoverflow替换。
  3. 对于出错的地方,对回声线做同样的事。

答案 3 :(得分:0)

由于您使用Wordpress id建议使用内置的wp_redirect();

http://codex.wordpress.org/Function_Reference/wp_redirect

if(isset($_GET['contactSubmit'])){
   $success = mail($emailTo, $subject, $body, $headers);
   if ($success){
     //comment out below...
     //echo '<p class="yay">All is well, your e&ndash;mail has been sent.</p>';
     wp_redirect(get_bloginfo('wpurl').'/your-thank-you-page/');
     exit;
   } 
} else {
  echo '<p class="oops">Something went wrong</p>';
}