2012-12-07 44 views
0

我可能在這裏錯過了一些非常簡單的東西,或者在我的代碼中有一個小錯字,但是我爲我建立的一個WordPress網站創建的PHP表單不會發送。如果表單中存在錯誤,驗證將起作用(除了您輸入的名稱外),但是當您正確填寫表單並單擊提交時,它將轉到其用於處理的頁面,並顯示「對不起,沒有帖子符合你的標準。'。另外郵件不會被髮送。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(); ?> 
+0

愚蠢的問題:使用定義的聯繫人模板的GET式觸摸頁? – maiorano84

+0

是的,它使用了正確的contact.php模板。 –

+1

我注意到,當您直接導航到「取得聯繫」頁面(不提交表單)時,頁面內容呈現沒有問題。但是,當表單被提交時,不能找到內容。如果你註釋掉'if(isset($ _POST ['submitted'])''塊之間的所有內容,會發生什麼?內容在提交表單時是否呈現?如果您在該塊中放置回聲語句,回聲語句是否會觸發? – maiorano84

回答

1

玉傢伙,

貌似我終於想通了這個問題。所有的代碼,所有它的變化都很好,而且工作,除了一件小事。 WordPress系統使用變量'$ name'來顯示博客名稱,因爲我使用該變量作爲我的表單上名稱字段的變量,所以它引發了問題。誰知道。

無論如何,我只是改變它使用'$ fullName',並將表單上的ID和名稱屬性更改爲'全名',一切都很好。

感謝@ 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(); ?> 
+0

哎唷!很難做到,做得很好,並感謝發佈解決方案。請記住將自己的答案標記爲正確,以便表明它已被解決。 – McNab

0

告訴我,如果是到達的郵件,如果不改變 $到= get_option( 'ADMIN_EMAIL' ); 與 $ to = [email protected]; //爲例

當你已經改變了嘗試,並告訴我,如果它的工作原理;)

+0

這不會影響任何東西,因爲我已經測試了$變量,它包含管理員電子郵件作爲一個字符串,所以它被設置和工作。 –

+0

另外,電子郵件沒有到達。正如我所說,表格不起作用。 –

+0

我剛試過的代碼等作品,而不是這個,: 如果(isset($到)||($來!== '')){$ 向= get_option( 'ADMIN_EMAIL'); } 僅使用:$到= 'youremail'; //如果 – Mitro