【问题标题】:WordPress PHP Form Won't SendWordPress PHP 表单不会发送
【发布时间】:2012-11-25 19:50:22
【问题描述】:

我可能在这里遗漏了一些非常简单的东西,或者我的代码中有一些错字,但是我为我正在构建的 WordPress 网站创建的 PHP 表单不会发送。如果表单中存在错误,则验证有效(除非您只输入姓名),但是当您正确填写表单并单击提交时,它只会转到要处理的页面并显示“对不起,不帖子符合您的标准。'。加上邮件没有发送出去。

编辑

开发站点的链接是http://dev.garethdaine.com/inkframe/

处理页面确实存在。这是我的代码:

表单代码

<form name="quick-contact" class="quick-contact" method="post" action="<?php bloginfo('url'); ?>/get-in-touch/">
    <h3>Request a Call Back</h3>
    <h4><label for="name">Name</label></h4>
    <input id="name" name="name" type="text" />
    <h4><label for="email">Email</label></h4>
    <input id="email" name="email" type="text" />
    <h4><label for="phone">Phone</label></h4>
    <input id="phone" name="phone" type="text" />
    <input id="submit" type="submit" value="Submit" />
    <input type="hidden" name="submitted" id="submitted" value="true" />
    <input type="hidden" name="required" id="required" class="required" />
</form>

PHP 代码

    <?php
    /*
    * Template Name: Contact
    */
    if( isset( $_POST['submitted'] ) ) {
        $to = get_option( 'admin_email' );

        if( trim( $_POST['name'] ) === '' ) {
            $nameError = 'You did not enter your name.';
            $hasError = true;
        } else {
            $name = trim( $_POST['name'] );
        }

        if( trim( $_POST['email'] ) === '' ) {
            $emailError = 'You did not enter your email address.';
            $hasError = true;
        } else if( !preg_match( "/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim( $_POST['email'] ) ) ) {
            $emailError = 'You entered an invalid email address.';
            $hasError = true;
        } else {
            $email = trim( $_POST['email'] );
        }

        if( trim( $_POST['phone'] ) === '' ) {
            $phoneError = 'You did not enter your telephone number.';
            $hasError = true;
        } else if( !is_numeric( $_POST['phone'] ) ) {
            $phoneError = 'You have entered an invalid phone number.';
            $hasError = true;
        } else {
            $phone = trim( $_POST['phone'] );
        }

        if( !empty( $_POST['required'] ) ) {
            $requiredError = 'You appear to be a robot. If you are human, please try again.';
            $hasError = true;
        }

        if( !isset( $hasError ) ) {
            $subject = 'Call Back Request from ' . $name;
            $body = "Name: $name \n\nEmail: $email \n\nPhone: $phone";
            $headers = 'From: ' . $name . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;

            wp_mail( $to, $subject, $body, $headers );
            $emailSent = true;
        }   
    }
?>
<?php get_header(); ?>
<div class="content" role="main">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>   
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <div class="entry-content">
                <?php if( isset( $emailSent ) && $emailSent == true ) { ?>
                    <p><strong>Your message has been sent successfully. Someone will be in touch shortly. Thanks.</strong></p>
                    <?php the_content(); ?>
                <?php } else { ?>
                    <?php if( isset( $hasError ) ) { ?>
                        <p class="error">Sorry, an error has occured.<p>
                        <ul>
                            <?php if( $nameError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $nameError; ?></span>

                                </li>
                            <?php } ?>
                            <?php if( $emailError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $emailError; ?></span>
                                </li>
                            <?php } ?>
                            <?php if( $phoneError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $phoneError; ?></span>
                                </li>
                            <?php } ?>
                            <?php if( !empty( $requiredError ) ) { ?>
                                <li>
                                    <span class="error"><?php echo $requiredError; ?></span>
                                </li>
                            <?php } ?>
                        </ul>
                    <?php } ?>
                    <?php the_content(); ?>
                <?php } ?>
            </div>
        </div>
    <?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

【问题讨论】:

  • 愚蠢的问题:您的联系页面是否使用定义的联系人模板?
  • 是的,它使用了正确的contact.php模板。
  • 我注意到当您直接导航到 Get In Touch 页面(不提交表单)时,页面内容呈现没有问题。但是,当提交表单时,找不到任何内容。如果您注释掉 if( isset( $_POST['submitted'] ) ) 块之间的所有内容会发生什么?内容是否在表单提交时呈现?如果您在该块中放置一个回显语句,回显语句会触发吗?
  • 尝试将内容类型和编码添加到标题中,看看是否有效 - $headers .= 'Content-type: text/html;字符集=utf-8\r\n';
  • @maiorano84 谢谢。不,echo 语句不输出任何内容。

标签: php forms wordpress email


【解决方案1】:

好吧,伙计们,

看来我终于找到了问题所在。所有的代码,在所有的变化中都很好并且可以工作,除了一件小事。 WordPress 系统使用变量“$name”来显示博客名称,当我将它用作表单上名称字段的变量时,它会导致问题。谁知道呢。

无论如何,我只是将其更改为使用“$fullName”并将表单上的 id 和 name 属性更改为“full-name”,一切正常。

感谢@maiorano84 为我指出了调试问题的正确方向。

不管怎样,下面是我修改后的代码,所以其他人可以使用它。

表单代码

<form name="quick-contact" class="quick-contact" method="post" action="<?php bloginfo( 'url' ); ?>/get-in-touch/">
    <h3>Request a Call Back</h3>
    <h4><label for="full-name">Full Name</label></h4>
    <input id="full-name" name="full-name" type="text" />
    <h4><label for="email">Email</label></h4>
    <input id="email" name="email" type="text" />
    <h4><label for="phone">Phone</label></h4>
    <input id="phone" name="phone" type="text" />
    <input id="submit" type="submit" value="Submit" />
    <input type="hidden" name="required" id="required" class="required" />
</form>

PHP 代码

<?php
    /*
    * Template Name: Contact
    */
?>
<?php get_header(); ?>
<?php
    if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
        $to = get_option( 'admin_email' );

        if( trim( $_POST['full-name'] ) === '' ) {
            $nameError = 'You did not enter your name.';
            $hasError = true;
        } else {
            $fullName = trim( $_POST['full-name'] );
        }

        if( trim( $_POST['email'] ) === '' ) {
            $emailError = 'You did not enter your email address.';
            $hasError = true;
        } else if( !preg_match( "/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim( $_POST['email'] ) ) ) {
            $emailError = 'You entered an invalid email address.';
            $hasError = true;
        } else {
            $email = trim( $_POST['email'] );
        }

        if( trim( $_POST['phone'] ) === '' ) {
            $phoneError = 'You did not enter your telephone number.';
            $hasError = true;
        } else if( !is_numeric( $_POST['phone'] ) ) {
            $phoneError = 'You have entered an invalid phone number.';
            $hasError = true;
        } else {
            $phone = trim( $_POST['phone'] );
        }

        if( !empty( $_POST['required'] ) ) {
            $requiredError = 'You appear to be a robot. If you are human, please try again.';
            $hasError = true;
        }

        if( !isset( $hasError ) ) {
            if ( !isset( $to ) || ( $to == '' ) ) {
                $to = get_option( 'admin_email' );
            }
            $subject = 'Call Back Request from ' . $fullName;
            $body = "Name: $fullName <br />Email: $email <br />Phone: $phone";
            $headers[] = 'From: ' . $fullName . ' <' . $email . '>';
            $headers[] = 'Reply-To: ' . $email;
            $headers[] = 'Content-type: text/html; charset=utf-8';

            wp_mail( $to, $subject, $body, $headers );
            $emailSent = true;
        }
    }
?>
<div class="content" role="main">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>   
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <div class="entry-content">
                <?php if( isset( $emailSent ) && $emailSent == true ) { ?>
                    <p><strong>Your message has been sent successfully. Someone will be in touch shortly. Thanks.</strong></p>
                <?php } else { ?>
                    <?php var_dump($headers); ?>
                    <?php if( isset( $hasError ) ) { ?>
                        <p class="error">Sorry, an error has occured.<p>
                        <ul>
                            <?php if( $nameError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $nameError; ?></span>

                                </li>
                            <?php } ?>
                            <?php if( $emailError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $emailError; ?></span>
                                </li>
                            <?php } ?>
                            <?php if( $phoneError != '' ) { ?>
                                <li>
                                    <span class="error"><?php echo $phoneError; ?></span>
                                </li>
                            <?php } ?>
                            <?php if( !empty( $requiredError ) ) { ?>
                                <li>
                                    <span class="error"><?php echo $requiredError; ?></span>
                                </li>
                            <?php } ?>
                        </ul>
                    <?php } ?>
                <?php } ?>
                <?php the_content(); ?>
            </div>
        </div>
    <?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

【讨论】:

  • 哎哟!很难,很好地深入了解它,并感谢发布解决方案。请记住将您自己的答案标记为正确,以表明它已解决。
【解决方案2】:

如果没有收到邮件,请告诉我,如果没有更改 $to = get_option('admin_email'); 和 $to = your@email.com; //示例

当你改变了尝试告诉我它是否有效;)

【讨论】:

  • 这不会影响任何事情,因为我已经测试了 $to 变量,它包含管理员电子邮件作为字符串,因此它已设置并可以正常工作。
  • 另外,没有收到电子邮件。正如我所说,表单不起作用。
  • 我刚刚尝试了代码并且可以正常工作,而不是这样: if ( !isset( $to ) || ( $to == '' ) ) { $to = get_option( 'admin_email ');仅使用:$to = 'youremail'; //不要使用那个 if
  • 即使进行了更改,代码仍然无法正常工作。我已经修改了上面的问题以显示完整的 contact.php 代码。
  • 你试过把完整的代码放在一个唯一的文件中吗?我试过了,它成功了
猜你喜欢
  • 2014-06-28
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 2014-02-01
  • 2018-01-24
相关资源
最近更新 更多