How to put a success alert on a data entry via ajax?

0

I'm moving on a project in progress and what I need to do in it is to put an alert and refresh the page after inserting data into the database. The data is being inserted through a worksheet, that is, each item is a row in the worksheet that contains as many columns. The way I put the alert is showing the alert for each item that is inserted successfully, that is, if the spreadsheet has 50 items will appear 50 alerts of success. I would like one to appear only at the end of all being inserted.

Code:

<script>

$("#salvar").click(function(){

    var indice = ${itens_camada.size()};
    var valores = "";
    var idCamada =  ${idCamada};
    var idInstituicao =  ${idInstituicao}
    var idMunicipio =  ${idMunicipio}

    for ( var i = 0; i < indice ; i++ ) {

    $("#item_"+i).closest('tr').find("input").each(function() {                                                       
        valores +=   this.value + "┆";                                                                                                                                                                                                                                      
    });

    var dados =  "idUsuario=1"
            +"&idCamada="+idCamada
            +"&idInstituicaoFonte="+idInstituicao
            +"&dataColeta=2016-11-17"
            +"&idMunicipio="+idMunicipio
            +"&camposInseridos="+valores.substr(0, valores.length - 1);

            sendPOST(dados, i);

            valores = "";
        }                                               
});

function sendPOST(dados, i){

    $.ajax( {
        url: "${wsadpa}importar/?"+ dados,                                 
        type: 'POST',                                                                                                    
        processData: false,  
        contentType: false,  

        success: function(data) {                                                                          
            Sucesso(i);
        }, 

        error: function (data, xhr, ajaxOptions, thrownError) {
            alert("Erro");                                        
        }       
    });                                                 
 }

 function Sucesso(i){
    $("#item_"+i).addClass("success");
    alert("Importação de Dados realizada com sucesso!"); // Aparece para cada item inserido
    location.reaload(); // Funciona OK                  
 }

 </script>
    
asked by anonymous 23.07.2017 / 01:49

1 answer

0
var qtdRegistros = dados.length;
var contador = 0; 
success: (function(data) { 
 contador++; 
 if (contador == dados.length) 
  alert('Mensagem');
});

Implements a counter and compares if the entered records are the same amount as your data.

    
04.05.2018 / 01:39