如何在同一页面上循环一一回响

how to echo one by one in loop on the same page

本文关键字:循环 一一 回响 一页      更新时间:2023-09-26

我正在编写一个程序,通过ajax向我们的注册用户发送批量电子邮件。

我希望在完成每个循环响应时回显它并进入下一个条件。

例如:-

我在数据库中有 100 封电子邮件的列表。当我提交编程请求时,它将开始发送电子邮件。

程序工作类似:

<?php
       foreach($emails as $email){
          $status = $this->sendMail($email);
          if($status == true)
          {
            echo "Mail Sent";
          }else{
            echo "Not Sent";
          }
       }
?>

现在我想为每个循环一次又一次地打印"邮件已发送"/"未发送"。

输出:-

已发送邮件

已发送邮件

已发送邮件

未发送

已发送邮件

发送。。

编辑

我的PHP代码是:-

<?php

    public function send_message() {
        $sendTo = $this->input->post('send_to');
        $template = $this->input->post('template');
        $subject = $this->input->post('subject');
        switch ($sendTo) {
            case 1:
                $users = $this->getAllEmails();
                break;
            case 2:
                $users = $this->getRegisteredUsersEmails();
                break;
            case 3:
                $users = $this->getTemproryUsersEmails();
                break;
            case 4:
                $users = $this->getSubscribersEmails();
                break;
        }
        $status = $this->sendMail($users, $template, $subject);
        echo "Mail Sent";
    }
 private function sendMail($users, $template, $subject) {
        $this->load->library('parser');
        $status = array();
        $i = 0;
        foreach ($users as $user) {
            $message = $this->parser->parse('email_templates/' . $template, array('email' => $user->email, 'name' => ($user->name != '') ? "Hi " . $user->name : "Hello"), TRUE);
            $response = $this->mail->send(config_item('sender_mail'), config_item('sender_name'), $user->email, $subject, $message);
            $status[$i]['email'] = $user->email;
            $status[$i]['status'] = ($response == 1) ? 1 : 0;
            $i++;
        }
        return $status;
    }
?>

我的阿贾克斯代码:-

<script type="text/javascript">
$("#send_mail").submit(function(){
    $.ajax{
    url:"<?php echo base_url('promotion/send_message'); ?>",
    type:"post",
    data:$(this).serialize(),
    success:function(data){
       $("#status").html(data);
    }
  }
});
</script>

你必须用javascript/jquery而不是PHP来做循环。要在服务器端没有溢出,您可能应该只使用递归在成功时调用函数。这样它将是同步的。

jQuery

var emails = [
'lorem@stackoverflow.com', 
'ipsum@stackoverflow.com', 
'foo@stackoverflow.com'
];
index = 0;
var sendMail = function(email){
  $.ajax({
    url:"sendMail.php",
    type: "POST"
    data: { emailId: email}  
    success:function(response) {
        index++;
        document.write(response); 
        if(emails[index] != undefined){
            sendMail(emails[index]);
        }
     }
  });
}
sendMail(emails[index]);

.PHP

$status = $this->sendMail($$_POST['email']);
$msg = $status ? "Mail Sent" : "Not Sent";
echo $msg;

I want to print the response when each time "$this->mail->send" function is called in "sendMail()"

正如上面的代码,$status应该像 json 对象一样在 ajax 函数中返回,array.so 我尝试这个......

private function sendMail($users, $template, $subject) {
    $this->load->library('parser');
    $status = array();
    $i = 0;
    foreach ($users as $user) {
        $message = $this->parser->parse('email_templates/' . $template, array('email' => $user->email, 'name' => ($user->name != '') ? "Hi " . $user->name : "Hello"), TRUE);
        $response = $this->mail->send(config_item('sender_mail'), config_item('sender_name'), $user->email, $subject, $message);
        $status[$i]['email'] = $user->email;
        $status[$i]['status'] = ($response == 1) ? 1 : 0;
        $i++;
    }
    echo json_encode($status);
}

阿贾克斯代码

<script type="text/javascript">
$("#send_mail").submit(function(){
$.ajax{
url:"<?php echo base_url('promotion/send_message'); ?>",
type:"post",
dataType : "json",
data:$(this).serialize(),
success:function(data){
    $.each(data,function(i,v){
       $("#status").append(v.status);
     }
}
}
});
</script>