-2

Ajax isn't working on a for loop. Can someone correct my code and please explain it to me. I just want to put those value on my database. I can only make it work if I call ajax once not like this, it is in a for loop.

var counter = $("input[name^= 'quantity']").length;
var array1 = $("input[name^= 'quantity']");
var array2 = $("input[name^= 'unit']");
var array3 = $("input[name^= 'item_description']");
var array4 = $("input[name^= 'stock_no']");
var array5 = $("input[name^= 'eunitcost']");
var array6 = $("input[name^= 'ecost']");

var i;

for (i = 0; i < counter; i++) {
  $.ajax({
    url: 'http://localhost/pm/admin/service/user-service.php',
    type: 'POST',
    dataType: 'json',
    data: {
      operation: 'pr-items',
      pr_no: $('#prno').val(),
      quantity: array1.eq(i).val(),
      unit: array2.eq(i).val(),
      item_description: array3.eq(i).val(),
      stock_no: array4.eq(i).val(),
      eunitcost: array5.eq(i).val(),
      ecost: array6.eq(i).val
    },

    success: function(data) {
      alert('pr items success');
      //todo
    },
    error: function(data) {
      // alert('pr items error');
      //todo
    }
  });
}
radscheit
  • 352
  • 4
  • 22
Peter Festejo
  • 97
  • 2
  • 2
  • 9
  • 4
    In what specific way is this failing? What's the actual problem? – David Aug 28 '18 at 17:34
  • 4
    Ideally you should only send in one AJAX request. Avoid looping and making an AJAX request for each record. Build an array and encode it as JSON. – dokgu Aug 28 '18 at 17:38
  • 1
    @PeterFestejo: You are missing some `()` after `val` in your `ecost` line. Does that help? – interfect Aug 28 '18 at 17:42
  • Try setting async:false in your ajax call – Sanjay Aug 28 '18 at 17:48
  • "my code isn't working" with no explanation of what "working" means. What is your desired result? What specifically is not working? Without that information, this question is off-topic and should be closed – mhodges Aug 28 '18 at 17:49
  • 5
    @Sanjay: That is *terrible* advice. And is likely to stop working in browsers soon if it hasn't already. – David Aug 28 '18 at 17:49
  • 2
    @David I wish I could upvote your comment a thousand times. – mhodges Aug 28 '18 at 17:50
  • @interfect well that works. But when i run it again it alert 'pr item error' when i remove the comment on it. – Peter Festejo Aug 28 '18 at 18:05
  • 1
    @PeterFestejo: If the AJAX operation is invoking the `error` callback then the server is returning an error. What is that error? Use your browser's debugging tools to observe the actual requests being sent to the server and the actual responses coming from the server. Check your server-side application's logs for error information. You need to *debug* to see what's happening. – David Aug 28 '18 at 18:19
  • Don't waste your time on this code because it is terrible performance-wise, learn how to pass a single array in a single request to the server – Alon Eitan Aug 28 '18 at 19:22

1 Answers1

0

I found out that the reason why my code above isnt working because ecost: array6.eq(i).val doesnt have a () after the .val and also I found out that this is not a good practice that having a ajax calls on loops. Im sorry if I can't explain well here in stackoverflow that resulting on downvotes and having a temporary ban. But well I deserve it. I just can't improve my questions.

Peter Festejo
  • 97
  • 2
  • 2
  • 9