2012-12-29 66 views
-5

我想從foreach循環中排除電子郵件代碼,此時電子郵件成功消息顯示3次。我試過不同的組合,但不能讓它只顯示一次...?在PHP中使用foreach循環 - wordpress

任何想法..?感謝

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

    $posted = $_POST; 
    $options = array('option-1' => __('Procedure: Suture Skin Wounds', 'appthemes'), 
      'option-2' => __('Procedure: Cat castration', 'appthemes'), 
      'option-3' => __('Procedure: Cat spay', 'appthemes') 
    ); 

    foreach ($options as $option => $value) { 

     if ($posted[$option] == "selected") { 
      echo "<li class='error-list'>"; 
      echo "Please select an answer for:" . " " . $value . " > 
    " . '<a href="' . get_permalink(30) . '">Back to form</a>'; 
      echo "</li>"; 
     }// if == "selected" 
     else/* if(isset($_POST['submit'])) */ { 

      $adminemail = get_option('admin_email'); 

      $ToEmail = $adminemail; 
      $EmailSubject = 'Skills Assessment Form'; 
      $mailheader = "From Leapfrog"; 
      $mailheader .= "Reply-To: " . $_POST["email"] . "\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Form:Small Animal\n"; 
      $MESSAGE_BODY = "Name: " . $_POST["canname"] . "\n\n"; 
      $MESSAGE_BODY .= "\n\n" . "Procedure: " . "Suture Skin Wounds: " . $_POST["option-1"]; 
      $MESSAGE_BODY .= "\n\n" . "Procedure: " . "Cat castration: " . $_POST["option-2"]; 
      $MESSAGE_BODY .= "\n\n" . "Procedure: " . "Cat spay: " . $_POST["option-3"]; 

      mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure"); 
      echo "Your skills assessment form has been Sent:" . " " . 
      '<a href="' . get_permalink(13) . '">Back to your Dashboard</a>'; 
      //echo '<a href="'.get_permalink(13).'">Dashboard</a>'; 
     }//else         
    }//foreach loop;  
}//if isset 
+1

您需要輸入真實的代碼。 – jeroen

+0

你的解釋沒有意義。代碼可能會有所幫助。 – eis

+0

我同意它看起來垃圾...! – Ledgemonkey

回答

1

你正在做foreach循環內的一切:顯示錯誤,郵件消息並顯示成功消息。

您需要將循環中的所有內容全部移出,例如在循環之前設置一個空的$errors數組並將所有錯誤消息添加到該數組。

然後,在循環之後,您可以檢查$errors數組是否仍爲空,併發送郵件並顯示您的成功消息。如果不是,則顯示所有錯誤。

+0

感謝您的答案 - 是的,我明白,最新的方法將錯誤添加到$錯誤數組.. ..? – Ledgemonkey

+0

@Ledgemonkey您不需要在循環中回顯您的錯誤,而只需將html字符串添加到數組中:'$ errors [] ='稍後要回顯的html',並且在循環之後通過'$ errors '並回應每一個元素。 – jeroen

+0

再次感謝您的幫助 – Ledgemonkey