1

私はおそらくここで本当に単純なものを見逃しているか、コードに少しタイプミスがありますが、私が構築している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(); ?>
4

2 に答える 2

1

OKみんな、

私はついに問題を理解したようです。すべてのコードは、すべてのバリエーションで問題なく機能しましたが、1つの小さなものを除きます。変数'$name'は、ブログ名を表示するためにWordPressシステムによって使用されます。これをフォームの名前フィールドの変数として使用していたため、問題が発生していました。誰かわかったね。

とにかく、「$ 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(); ?>
于 2012-12-09T15:06:08.790 に答える
0

メールが届いたかどうかを教えてください。届かなかった場合は、$ to = get_option('admin_email');を変更してください。$ to = your@email.com; //例

変更したら、うまくいくかどうか教えてください;)

于 2012-12-07T17:19:59.847 に答える