2013-06-05 85 views
1

我在教程的幫助下設計了聯繫表格模板,然後將模板添加到頁面中。如果任何字段爲空或顯示成功消息,我試圖顯示錯誤消息。它正確顯示錯誤信息,但是當輸入所有字段時,它顯示一個空白頁。無法弄清楚我做錯了什麼。這是我的整個代碼。wordpress聯繫表格模板

<?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();?> 

回答

1

首先將此行添加到腳本的頂部。

$hasError = false; 

然後使用此代碼塊來顯示成功消息。

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

希望這可以幫助你。

+0

非常感謝。這是一個非常小的修復和工作。 – user1999205

+0

請記住,如果您有解決方案,請將該答案標記爲已接受。 謝謝, – Bilal