1

我在教程的帮助下设计了一个联系表单模板,然后将模板添加到页面中。如果任何字段为空,我将尝试显示错误消息或显示成功消息。它正确显示错误消息,但是当输入所有字段时,它显示一个空白页。无法弄清楚我做错了什么。这是我的整个代码。

<?php
/*
Template Name: Contact
*/

?>

<?php
$msgSubmission = false;
$nameError = '';
$emailError = '';
$commentError = '';

if(isset($_POST['submitted'])) {

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

    if(trim($_POST['email']) === '')  {
        $emailError = 'Please 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['comments']) === '') {
        $commentError = 'Please enter a message.';
        $hasError = true;
    }else if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['comments']));
    } else {
        $comments = trim($_POST['comments']);
    }

    if((isset($name) && $name!='') && (isset($email) && $email!='') && (isset($comments) && $comments!='')) {
        $msgSubmission = true;
    }

} ?>

<?php get_header();?>
<div id="post_container">
    <div id="left_part">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <div class="entry-content">
                <?php if(isset($msgSubmission) && $msgSubmission == true) { ?>
                <div class="thanks"><br /><br />Thanks, your message was sent successfully.<br /><br /></div>
                <?php } else { ?>
                <?php the_content(); ?>

                <form action="<?php the_permalink()?>" id="contactForm" method="post">
                <ul>
                    <li>
                        <!--    <label for="contactName">Name:</label>  -->
                        <?php if($nameError != '') { ?>
                        <span class="errorMsg"><?php echo $nameError; ?></span>
                        <?php } ?>
                        <input type="text" class="txtinput" placeholder="Your name" name="name" id="name" value="<?php if(isset($_POST['name']))  echo $_POST['name'];?>" />
                    </li>
                    <li>
                        <!--    <label for="email">Email</label>    -->
                        <?php if($emailError != '') { ?>
                        <span class="errorMsg"><?php echo $emailError;?></span>
                        <?php } ?>
                        <input type="text" class="txtinput" placeholder="Your e-mail address" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" />
                    </li>
                    <li>
                        <!--    <label for="commentsText">Message:</label>  -->
                        <?php if($commentError != '') { ?>
                        <span class="errorMsg"><?php echo $commentError;?></span>
                        <?php } ?>
                        <textarea class="txtblock" placeholder="Enter a cool message..." name="comment" id="comment" rows="15" cols="55">
                        <?php if(isset($_POST['comment']))  echo $_POST['comment'];?>
                        </textarea>
                    </li>
                    <li>
                        <input type="hidden" name="submitted" id="submitted" value="true" />
                        <input type="submit" class="submit" value="Submit Message!"></input>
                    </li>
                </ul>
                </form>
                <?php } ?>
            </div><!-- .entry-content -->
        </div><!-- .post -->
        <?php endwhile; endif; ?>
    </div><!-- .left_part -->
    <?php get_sidebar();?>
    <div class="separator"></div>
</div>
<?php get_footer();?>
4

1 回答 1

1

首先将此行添加到脚本的顶部。

$hasError = false;

然后在要显示成功消息的地方使用此代码块。

if(!$hasError) {
// display success message.
}

希望这可以帮助你。

于 2013-06-05T07:22:44.883 回答