如何在foreach循环中回显消息一次

时间:2018-06-14 15:50:25

标签: php jquery ajax

假设我有这样的查询:

    foreach ($emailist AS $addy) {
     if(@mail($to, $subject, $message, $headers))
    {
    echo "Mail Sent";
    }
    else
    {
    echo "e";
    }   
  }

我有一个调用响应的jquery

success :  function(data)
{
if(data=="Mail Sent")
{
$("#error").fadeIn(1000, function(){
$("#error").html('<div class="alert alert-success"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Successfully Sent!!</div>');
$("#btn-submit").html('<span class="fa fa-check"></span> &nbsp; Sent');

});
}
else if(data=="e")
{
$("#error").fadeIn(1000, function(){
$("#error").html('<div class="alert alert-success"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Saved only to database and mail not sent!!</div>');
$("#btn-submit").html('<span class="fa fa-check"></span> &nbsp; Sent');

});
} }

如何在这样的循环中使邮件发送回显一次?

我已尽力而为但无济于事。每次我运行程序邮件已发送显示程序循环的时间,而不是ajax响应中的成功发送!!

1 个答案:

答案 0 :(得分:1)

看起来您可能正在尝试查看是否所有电子邮件都已成功发送?

$errorsExist = false;

foreach ($emailist AS $addy) {
    //errors exist if the email fails now, or did in the past
    $errorsExist = !@mail($to, $subject, $message, $headers) || $errorsExist;
}

if ($errorsExist) {
    echo "e";
} else {
    echo "Mail Sent";
}