联系表单提交后如何重定向到感谢页面 - Wordpress 高级主题联系我们 php 模板 [关闭]
Posted
技术标签:
【中文标题】联系表单提交后如何重定向到感谢页面 - Wordpress 高级主题联系我们 php 模板 [关闭]【英文标题】:How to redirect to a thankyou page after contact form submission - Wordpress premium theme contact us php template [closed] 【发布时间】:2013-02-09 19:04:47 【问题描述】:我正在开发一个 Wordpress 主题,我正在尝试让主题随附的自定义联系表单在成功完成后重定向到感谢页面,以便我可以使用 adwords 转换跟踪。
虽然我已经查看了 Stackflows 以前关于此主题的问题/答案,但我仍在努力寻找解决问题的正确代码/解决方案(坦率地说,我对代码知之甚少)。对于这方面的任何帮助,我将非常感激!这是代码...
<?php
/*
Template Name: Contact Form
*/
?>
<?php
global $woo_options;
$nameError = '';
$emailError = '';
$commentError = '';
//If the form is submitted
if(isset($_POST['submitted']))
//Check to see if the honeypot captcha field was filled in
if(trim($_POST['checking']) !== '')
$captchaError = true;
else
//Check to make sure that the name field is not empty
if(trim($_POST['contactName']) === '')
$nameError = __( 'You forgot to enter your name.', 'woothemes' );
$hasError = true;
else
$name = trim($_POST['contactName']);
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === '')
$emailError = __( 'You forgot to enter your email address.', 'woothemes' );
$hasError = true;
else if (!eregi( "^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]2,4$", trim($_POST['email'])))
$emailError = __( 'You entered an invalid email address.', 'woothemes' );
$hasError = true;
else
$email = trim($_POST['email']);
//Check to make sure comments were entered
if(trim($_POST['comments']) === '')
$commentError = __( 'You forgot to enter your comments.', 'woothemes' );
$hasError = true;
else
if(function_exists( 'stripslashes'))
$comments = stripslashes(trim($_POST['comments']));
else
$comments = trim($_POST['comments']);
//If there is no error, send the email
if(!isset($hasError))
$emailTo = get_option( 'woo_contactform_email' );
$subject = __( 'Contact Form Submission from ', 'woothemes' ).$name;
$sendCopy = trim($_POST['sendCopy']);
$body = __( "Name: $name \n\nEmail: $email \n\nComments: $comments", 'woothemes' );
$headers = __( 'From: ', 'woothemes') . "$name <$email>" . "\r\n" . __( 'Reply-To: ', 'woothemes' ) . $email;
//Modified 2010-04-29 (fox)
wp_mail($emailTo, $subject, $body, $headers);
if($sendCopy == true)
$subject = __( 'You emailed ', 'woothemes' ).get_bloginfo( 'title' );
$headers = __( 'From: ', 'woothemes' ) . "$name <$emailTo>";
wp_mail($email, $subject, $body, $headers);
$emailSent = true;
?>
<?php get_header(); ?>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery(document).ready(function()
jQuery( 'form#contactForm').submit(function()
jQuery( 'form#contactForm .error').remove();
var hasError = false;
jQuery( '.requiredField').each(function()
if(jQuery.trim(jQuery(this).val()) == '')
var labelText = jQuery(this).prev( 'label').text();
jQuery(this).parent().append( '<span class="error"><?php _e( 'You forgot to enter your', 'woothemes' ); ?> '+labelText+'.</span>' );
jQuery(this).addClass( 'inputError' );
hasError = true;
else if(jQuery(this).hasClass( 'email'))
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]2,4)?$/;
if(!emailReg.test(jQuery.trim(jQuery(this).val())))
var labelText = jQuery(this).prev( 'label').text();
jQuery(this).parent().append( '<span class="error"><?php _e( 'You entered an invalid', 'woothemes' ); ?> '+labelText+'.</span>' );
jQuery(this).addClass( 'inputError' );
hasError = true;
);
if(!hasError)
var formInput = jQuery(this).serialize();
jQuery.post(jQuery(this).attr( 'action'),formInput, function(data)
jQuery( 'form#contactForm').slideUp( "fast", function()
jQuery(this).before( '<p class="tick"><?php _e( '<strong>Thanks!</strong> Your email was successfully sent.', 'woothemes' ); ?></p>' );
);
);
return false;
);
);
//-->!]]>
</script>
<div id="content" class="col-full">
<div id="main" class="col-left">
<?php if ( isset($woo_options[ 'woo_breadcrumbs_show' ]) && $woo_options[ 'woo_breadcrumbs_show' ] == 'true' ) ?>
<div id="breadcrumbs">
<?php woo_breadcrumbs(); ?>
</div><!--/#breadcrumbs -->
<?php ?>
<div id="contact-page" class="post">
<?php if(isset($emailSent) && $emailSent == true) ?>
<p class="info"><?php _e( 'Your email was successfully sent.', 'woothemes' ); ?></p>
<?php else ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<div class="entry">
<?php the_content(); ?>
</div>
<?php $geocoords = $woo_options['woo_contactform_map_coords']; ?>
<?php if ($geocoords != '') ?>
<?php woo_maps_contact_output("geocoords=$geocoords"); ?>
<?php echo do_shortcode( '[hr]' ); ?>
<?php ?>
<?php if(isset($hasError) || isset($captchaError) ) ?>
<p class="alert"><?php _e( 'There was an error submitting the form.', 'woothemes' ); ?></p>
<?php ?>
<?php if ( get_option( 'woo_contactform_email') == '' ) ?>
<?php echo do_shortcode( '[box type="alert"]'.__( 'E-mail has not been setup properly. Please add your contact e-mail!', 'woothemes' ).'[/box]' ); ?>
<?php ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ol class="forms">
<li><label for="contactName"><?php _e( 'Name', 'woothemes' ); ?></label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" />
<?php if($nameError != '') ?>
<span class="error"><?php echo $nameError;?></span>
<?php ?>
</li>
<li><label for="email"><?php _e( 'Email', 'woothemes' ); ?></label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="txt requiredField email" />
<?php if($emailError != '') ?>
<span class="error"><?php echo $emailError;?></span>
<?php ?>
</li>
<li class="textarea"><label for="commentsText"><?php _e( 'Message', 'woothemes' ); ?></label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) if(function_exists( 'stripslashes')) echo stripslashes($_POST['comments']); else echo $_POST['comments']; ?></textarea>
<?php if($commentError != '') ?>
<span class="error"><?php echo $commentError;?></span>
<?php ?>
</li>
<li class="inline"><input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> /><label for="sendCopy"><?php _e( 'Send a copy of this email to yourself', 'woothemes' ); ?></label></li>
<li class="screenReader"><label for="checking" class="screenReader"><?php _e( 'If you want to submit this form, do not enter anything in this field', 'woothemes' ) ?></label><input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking'])) echo $_POST['checking'];?>" /></li>
<li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><input class="submit button" type="submit" value="<?php esc_attr_e( 'Submit', 'woothemes' ); ?>" /></li>
</ol>
</form>
<?php endwhile; ?>
<?php endif; ?>
<?php ?>
</div><!-- /#contact-page -->
</div><!-- /#main -->
<?php get_sidebar(); ?>
</div><!-- /#content -->
<?php get_footer(); ?>
抱歉发布所有代码(与没有线索有关),但我不确定哪些位相关/需要更改。非常感谢您的帮助!
【问题讨论】:
相关:How to validate an email address in PHP 【参考方案1】:您究竟想达到什么目标?发送确认电子邮件后,您想将用户重定向到一个完全不同的页面吗? 你可以在 PHP 中做到这一点,它就像替换行一样简单:
$emailSent = true;
类似这样的:
header('Location: http://www.mysite.com/thankyoupage.htm');
exit();
或者您可以在显示“电子邮件已成功发送”消息几秒钟后使用 javascript/jquery 重定向用户。
【讨论】:
我正在尝试将客户从联系我们页面(完成后)重定向到感谢页面,以便在 google adwords 中设置转化跟踪。按照你的建议换掉上面的代码似乎没有用。 您能否详细说明“似乎不起作用”?您是否创建了thankyou.htm 页面并在其中插入了adwords 转换跟踪sn-p?用户是否会被重定向到thankyou.htm 页面? 对不起,如果不清楚!,我在这方面完全是菜鸟!是的,我创建了一个感谢页面并插入了跟踪 sn-p。目前,一旦发送了他们的联系方式,用户就不会被重定向到该页面(这是我想要实现的)。目前,一旦发送详细信息,客户将在联系页面上输出感谢。 我似乎没有像我用我的主页 url 测试它那样工作,但是,没有重定向,在与联系表单所在的页面相同的页面上输出相同的文本输出“感谢您联系我们” 好了,就是用ajax提交表单并获取结果。您需要使用 javascript 进行重定向。尝试使用这个jQuery.post(jQuery(this).attr('action'),formInput, function(data) window.location.replace('http://www.mysite.com/thankyou.htm'); );
当然你必须替换jQuery.post(...
之后的现有代码以上是关于联系表单提交后如何重定向到感谢页面 - Wordpress 高级主题联系我们 php 模板 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章