2012-12-29 65 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