Alert being repeated through . each() ajax

Asked

Viewed 57 times

1

I got the following jQuery:

$("#apagar-marcados").click(function(){
    var lista_marcados = document.getElementsByName('id_mensagem[]');
    $.each(lista_marcados, function(i, item){
        if ($(item).prop('checked')){
            $.ajax({
                type: "POST",
                url: BASE_URL + "ajax/remover_mensagem",
                data:{id_mensagem: $(item).val()}, 
                cache: false,

                success: function(){
                    alert('atualizado')
                }
            });                 
        }
    });
});

In this, I update via SQL in the database, until then, works perfectly. In this case, I have a checkbox listing, which in turn, marked, with the click of the delete button, will enter this action.

The problem is, if I select 10 checkbox, the alert('atualizado') 10x, as I do to appear only once?

  • 3

    The return of function $.ajax is a Promise, then you can try using the Promise.all(), but why not send all values in the same request already?

  • Because I don’t know how to do this @Andersoncarloswoss, in PHP I know how to update everyone who comes in the post(id_message) through the array, but I don’t know how to pass the array().

1 answer

4


You should consider only sending 1 ajax to the server and letting the server make the changes to the database. If that application is large and you have many users sending N ajax that way it can be heavy for the server to manage.

To send only 1 ajax you could do so:

$("#apagar-marcados").click(function() {
  var lista_marcados = document.getElementsByName('id_mensagem[]');
  var marcados = [].filter.apply(lista_marcados, function(item) {
    return item.checked;
  }).map(function(item) {
    return item.value;
  });

  $.ajax({
    type: "POST",
    url: BASE_URL + "ajax/remover_mensagem",
    data: {marcados: marcados},
    cache: false,
    success: function() {
      alert('atualizado')
    }
  });

});

But in answer to your question you can ask like this:

$("#apagar-marcados").click(function() {
  var items = $('[name="id_mensagem[]"]').get();
  var ajaxs = items.reduce(function(arr, item) {
    if (!item.checked) return arr;
    const ajax = $.ajax({
      type: "POST",
      url: BASE_URL + "ajax/remover_mensagem",
      data: {id_mensagem: item.value},
      cache: false
    });
    return arr.concat(ajax);
  }, []);

  $.when.apply($, ajaxs).done(function() {
    alert('Concluído!');
  });
});
  • this way, you will be sending by array() the items or sending in the way I already do today?

  • 1

    @Andrébaill this is how you do today (as in the question).

  • Okay, so it worked perfectly, but @Andersoncarloswoss told me that I should send everything together, to PHP Treat and update. How would it be done?

  • @Andrébaill added to the answer.

  • 1

    Perfect @Sergio, very grateful, that’s right! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.