为每个数据发送追加 Ajax 多次响应

Append Ajax Response Multiple Times Foreach Data Send

我有一份等待付款的用户列表。我想为有待处理的用户创建批量付款。

我为此创建了 ajax 请求

<script>
jQuery(document).ready(function(){
    $("#boxesAll").click(function () {
        $('input[name="boxes[]').prop('checked', this.checked);
            $submit_act.toggle( $submit_box.is(":checked") );
          $submit_act_paid.toggle( $submit_box.is(":checked") );
        if($(this).is(':checked')){
            var numberChecked = $('input[name="boxes[]"]:checked').length;
            $('#count_item').html(numberChecked +' items');
            $('tr>td:first-child').addClass('bg-warning');
        } else {
            $('tr>td:first-child').removeClass('bg-warning');
        }
    });

    var id,resp,
      $submit_act = $(".btn-act").hide(),
      $count_item = $("#count_item").hide(),
      $submit_box = $('input[name="boxes[]"]').click(function() {
        $submit_act.toggle( $submit_box.is(":checked") );
        $count_item.toggle( $submit_box.is(":checked") );
    $('input[name="boxes[]"]').change(function() {
      $(this).parents('tr>td').toggleClass('bg-warning', $(this).is(':checked'));
    });
    var numberChecked = $('input[name="boxes[]"]:checked').length;
    $('#count_item').html(numberChecked +' items selected');
    });

  $('.btn-act').click(function(){
    var btnid = jQuery(this).attr("id");
    var page = '<?=$page;?>';
    id = [];
    $(':checkbox:checked').each(function(i){
      id[i] = $(this).val();
    });  
    if (confirm("Are you sure to "+btnid+" these items?")) {
     if(id.length === 0){
      alert("Please Select atleast one item(s).");
     }else{
        $.ajax({
            type: 'POST',
            url: '/<?=$settings['admin_dir'];?>?p=jquery&act='+btnid,
            data: {page:page,id:id},
            beforeSend: function() {
                $(".btn-act").attr("disabled", true);
                $(".btn-act").html('<span class="spinner-border spinner-border-sm align-middle" role="status" aria-hidden="true"><span class="sr-only">Loading...</span></span> Please wait...');
            }
        }).done(function(t) {
            setTimeout(function() {  
                data = JSON.parse(t);
                if(data['result']=='success'){
                  var pages = "<?=$settings['admin_dir'];?>?p="+page;
                  if(data['type']=='payments'){
                    $(".btn-act").html('All Payment Paid');
                    $(".btn-act").attr("disabled", true);

                     for(var i=id.length; i>0; i--){
                      $("#action_response").append('<div id="resp">'+data['msg']+'</div>');
                     }
                      
                  }
                }else{
                    $(".btn-act").attr("disabled", false);
                    $(".btn-act").html('Failed to action');
                } 
            }, 3000);
            });
    }
    }
  });
});
</script>

然后 php 处理它

if($_GET['act']=="payments"){
    if(isset($_POST["page"]) && isset($_POST["id"])){
      foreach($_POST["id"] as $id){
        $details = getDetails('user_payments','id',$id);
        $user = getDetails('users','id',$details["user_id"]);
      }
            $msg = array(
              "result" => 'success',
              "type" => 'payments',
              "msg" => 'Successfully paid to '.$user['username']
            );
        echo json_encode($msg);
    }
  }

所有代码都有效,但我对结果有疑问。结果总是 return 相同的结果。

例如我检查了 4 项付款,结果在这里

Successfully paid to user1
Successfully paid to user1
Successfully paid to user1
Successfully paid to user1

我想要的结果是追加我检查的每个用户。

Successfully paid to user1
Successfully paid to user2
Successfully paid to user3
Successfully paid to user4

所以我们知道哪个用户已成功付款。 让我知道是否有人可以解决此问题

您在 ajax 回复中只有 return 一条消息。
要获得多个结果,您必须在循环中构建消息

if($_GET['act']=="payments"){
  if(isset($_POST["page"]) && isset($_POST["id"])){
    $msg = [];
    foreach($_POST["id"] as $id){
      $details = getDetails('user_payments','id',$id);
      $user = getDetails('users','id',$details["user_id"]);
      $msg[] = 'Successfully paid to '.$user['username'];
    }
    $response = array(
      "result" => 'success',
      "type" => 'payments',
      "msg" => $msg
    );
    echo json_encode($response);
  }
}

.

for(var i=id.length; i>0; i--){
  $("#action_response").append('<div id="resp">'+data.msg[i]+'</div>');
}