WordPress PHP表单将无法发送

时间:2012-12-07 15:49:10

标签: php forms wordpress email

我可能在这里遗漏了一些非常简单的东西,或者在我的代码中有一点错字,但我为我正在构建的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(); ?>

2 个答案:

答案 0 :(得分: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(); ?>

答案 1 :(得分:0)

告诉我是否收到了电子邮件,如果没有更改 $ to = get_option('admin_email'); 同 $ to = your@email.com; //为例

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