Delete record without page refresh?

0

I have the following Ajax / Jquery to delete a record according to the action of a href.

<script type="text/javascript">
    function apagarRegistro(idTel) {

            var baseurl = '<?php echo $sig_url;?>';

                $.ajax ({

                    url: baseurl+"/sistel/index/apagar?idRegistro="+idTel,
                    type:'POST',

                success:function(res){

                    if (res != 'success') {
                         alert("Registro apagado com sucesso.");
                         history.go(0);
                    }
                }

               });
    }
</script>

The problem I am having is that after I make a query on the first page (of pagination) when I click on delete the file and it is done successfully, I want to be given the refresh on the page however it presents the message to confirm as print there:

What would be the solution to this?

    
asked by anonymous 13.02.2015 / 13:51

1 answer

1

The question is, how are you doing to display the table? In a pagina.php only, or via ajax , mounting the data dynamically ....

  • If mounted on a single page, the solution would be to delete the line where are the data.

  • Now if you are mounting the table via Ajax , just call
    again the function that mounts the table, there the data saw
    updated ...

Mounted Table on Page:

function apagarRegistro(idTel){
.
.
.
    success:function(res){
        if (res != 'success') {
            var par = $(this).parent().parent(); //tr
            par.remove();
            alert("Registro apagado com sucesso.");
        }
    }
.
.
.
}

Table mounted by function AJAX :

In a file arquivo.html I have the following script:

<script>
function carregaTabela(){
    $.ajax({
        type: "POST",
        url: "tabela.php",
        success: function(data) {
            $('#tabela').html(data);
        }
    });
}

function excluir(id){
    resposta = confirm("Deseja realmente excluir esse aluno?");
    if (resposta){
        $.ajax({
            type: "POST",
            data: {
                id: id
            },
            url: "php/acao.php",
            success: function(data) {
                if(data == 1){
                    alert("Excluido com Sucesso!");
//Nessa linha é feita a mágica, toda vez que excluir o registro, ele chamara a função  carregaTabela() novamente
                    carregaTabela();
                }else{
                    alert("Houve algum erro ao excluir!");
                }
            },
            error: function(){
                alert("Houve algum erro ao excluir!");
            }
        });
    }
}

</script>
<body onload="carregaTabela()">
<div id="tabela"></div>
</body>

My page tabela.php is simple, it only has the table formatted with the data inside, in addition in each line of foreach I add the following column:

<td style='text-align: center;'><img style='cursor:pointer;' src='delete.png' onclick='excluir(".$id.",".$varIDTeste.")' title='Excluir'></td>
    
13.02.2015 / 15:09