【问题标题】:Contact page template - redirect to 'thank you page' not working联系页面模板 - 重定向到“感谢页面”不起作用
【发布时间】:2012-09-26 07:42:40
【问题描述】:

我正在尝试通过自定义页面模板提交表单,但问题是它仅适用于 form action="<?php the_permalink() ?>",我需要提交表单并将其重定向到类似 form action="<?php bloginfo('url')?>/message-sent?id=<?php the_ID() ?>" 的内容

完整代码:

<?php 
$emailError = '';
if(isset($_POST['submitted'])) {

            $email = trim($_POST['email']);



                //setup self email address
                $emailTo = $email; 


            $subject = "[reminder] Don't forget to download " . get_the_title();
            $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
            $headers = 'From: Myemail reminders <no-reply@xyz.com>' . "\r\n";
            wp_mail($emailTo, $subject, $body, $headers);
            $emailSent = true;

} ?>




<section class="box grid_9 list_posts">
<div class="inner">
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry-content">
<div class="contact-form clearfix">
                            <?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<?php _e('Thanks, your email was sent successfully.', 'framework') ?>
</div>
                            <?php } else { ?>
                                <?php the_content(); ?>
                                <?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error"><?php _e('Sorry, an error occured.', 'framework') ?>

                                <?php } ?>






<form action="<?php the_permalink()?>" id="contactForm" method="post">
<ul class="contactform">



<li>
                                            <input type="email" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" required="required" />
                                        </li>

<li class="buttons">

                                            <input type="hidden" name="submitted" id="submitted" value="true" />
                                            <input type="submit" value="Remind Me!"></input>
                                        </li>
</ul></form>






                            <?php } ?></div>
</div>
</div>
                    <?php endwhile; else: ?>
<div id="post-0" <?php post_class() ?>>
<h1 class="entry-title"><?php _e('Error 404 - Not Found', 'framework') ?></h1>

</div>
                <?php endif; ?></div>
</section>

我在日志中没有 php 错误,页面重定向成功,但没有发送电子邮件。使用 the_permalink 时,一切正常。

【问题讨论】:

    标签: forms wordpress email contact


    【解决方案1】:

    将表单数据提交到不同的脚本时,请确保(验证输入和)发送电子邮件的代码就在该文件中。

    否则,您的 URL /message-sent 可能会重写为完全不同的脚本,并且一旦单击提交按钮,则根本不涉及包含上述代码的脚本。

    这让你受不了了吗?如果我的措辞难以理解或者我的描述对你来说不是很清楚,请随意提问

    【讨论】:

    • 我不确定我是否正确,但验证码应该转到 /message-sent 页面吗?对不起,我还在学习 php 语言。
    • 无论您在表单的action 属性中输入什么,都将是表单提交后要访问的 URL。在你的情况下,那将是/message-sent。这应该是验证输入和发送电子邮件的地方(脚本)。
    • 太棒了!感谢您快速且易于理解的回复。现在一切正常。
    • 但我实际上会建议另一种方法。将表格发送到永久链接。这样,您可以将有关表单的任何内容保存在一个地方。如果电子邮件发送成功,然后执行 HTTP 重定向到 /thank-you 页面。这样,您在浏览器刷新问题方面也会更加安全。
    【解决方案2】:

    也许您忘记在/message-sent?id=xxx 文件的末尾添加“.php”,即/message-sent.php?id=xxx

    另一个想法:过滤用户输入总是一个好主意,因为您会收到很多垃圾邮件,放置某种 CAPTCHA 验证代码并清理/验证整个用户输入文本,即每个文本,来自表单的输入字段。

    对于电子邮件:

    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    

    对于名称和 cmets:

    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    $comments = filter_var(strip_tags($_POST['comments']), FILTER_SANITIZE_STRING);
    

    【讨论】:

    • 这不是问题,因为它是一个 wordpress 页面,所以它只能用作 '/message-sent?id=xxx'
    • 我没有进行任何验证(HTML5 电子邮件字段除外)的原因是,它将是一个提醒服务,它将向提供必要信息的地址发送一封电子邮件,以便人们不会不要忘记稍后回来下载文件。
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多