Delete record without page refresh?

Asked

Viewed 2,210 times

0

I have the following Ajax/Jquery to delete a record as an 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’m having is that after I make a query on the first page (of the pagination) when I click to delete the file and the same is done successfully, I want to be given the refresh on the page but it presents the message to confirm as print there:

inserir a descrição da imagem aqui

What would be the solution to this?

  • Why you need to give the refresh? If it is to update the deleted record it would be better to update by own ajax.

  • Yeah, somehow I messed up then. Because the code should do it. What happens is that if I don’t refresh the record, it doesn’t go away. #buguei

  • If the table where the record is being called via ajax, just call the function that creates it again.

  • If the data is in a table, just delete the row on which the deleted file stood. It all depends on how you are presenting this data.

  • I don’t understand well, in my case I’m using Zend and I have a method in the model that makes delete.

  • Shows the code where Voce mounts the table to better elucidate the question

  • The way to bring data is normal with foreach.. the line that does delete action is the one that as loop has the ID of each record: <?php href="javascript:eraserRegistro(<? php echo $res->idTelefone;?>)" ?>

Show 2 more comments

1 answer

1

The question is, how are you doing to display the table? In a pagina.php alone, or via ajax, assembling 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, ai the data turn
    up-to-date...


Table mounted on page:

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

Mounted table by function AJAX:

In an archive 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, just has the table formatted with the data inside, in addition in each row of the 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>

Browser other questions tagged

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