How to put a successful Alert into a data insertion via ajax?

Asked

Viewed 80 times

0

I am working on a project in progress and what I need to do on it is put an Alert and refresh the page after entering data in the database. The data are being entered through a spreadsheet, that is, each item is a row of the spreadsheet that contains other many columns. The way I put Alert it is showing Alert for each item that is successfully inserted, that is, if the spreadsheet has 50 items it will show 50 successful Alerts. I wish only one appeared at the end of all being entered.

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>
  • Try to create a counter for each saved record, then in Success you do an if(counter == numberLines) { Alert('Saved') }

1 answer

0

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

Implement a counter and compare if the entered records are the same amount as your data.

Browser other questions tagged

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