Exclusion ajax + jquery

Asked

Viewed 315 times

0

Which correct method to be able to delete, my doubt is being how you get the id based on the table row, and how to send it to a page that actually excludes. The codes I’ve already made will be following.

listUsuario.php

    <script type="text/javascript">
$("#elemento-pai-da-tabela").on('click', '.btn_Excluir', function(e){
        e.preventDefault(); 
        var $trPai = $(this).parent().parent(); 
        var id = $trPai.data('id'); 
        $.ajax({ type: "POST", 
            data: { 'COD_IDENT_USUAR': id }, 
            url: "excluirUsuario.php", 
            success: function(msg) { 
                $trPai.remove(); 
            } 
    }); 
}); 
</script>
<?php

    // incluindo o arquivo que faz a conexao com o banco
    include ("../includes/conexao.php");

    // Listando os Cargos

    // executa  query de consulta e armazena o resultado devolvido na variável $resultado
     $resultado = mysql_query("SELECT TXT_NOMEX_USUAR, TXT_ENDER_EMAIL, FLG_STATU_USUAR, DAT_ULTIM_LOGIN, COD_IDENT_USUAR from tbl_USUARIOS");


    // se não existir cargos cadastrados exibe uma mensagem
    if(mysql_num_rows($resultado)<=0)
    {
        echo "<div class='alert alert-error'>";
        echo "<b>Atenção!</b><br>";
        echo "Não existe cargo cadastrado no momento.";
        echo "</div>";
    }

    // se existir produtos cadastrados lista-os
    else
    {
        echo "<table class='table table-striped'>";
        echo "<th class='excluir'>Excluir</th>";
        echo "<th class='alterar'>Alterar</th>";
        echo "<th>Nome</th>";
        echo "<th>Email</th>";
        echo "<th>Status</th>";
        echo "<th>Último Login</th>";

        while($linha = mysql_fetch_array($resultado))
        {
            echo "<tr data-id='$linha[4]'>";
            echo "<tr>";
            echo "<td><a href='excluirUsuario.php' class='btn_Excluir'><i class='glyphicon glyphicon-remove'></i></a></td>";
            echo "<td><a href='excluirUsuario.php' class='btn_Alterar'><i class='glyphicon glyphicon-pencil'></i></a></td>";
            echo "<td>$linha[0]</td>";
            echo "<td>$linha[1]</td>";
            echo "<td>$linha[2]</td>";
            echo "<td>$linha[3]</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    mysql_close($conn);
?>

excluiUsuario.php

<?php
     // incluindo o arquivo que faz a conexao com o banco
    include ("../includes/conexao.php");

    // Recebendo o valor enviado pelo link
    $COD_IDENT_USUAR = isset($_POST['COD_IDENT_USUAR']) ? $_POST['COD_IDENT_USUAR'] : '';

    mysql_query("DELETE FROM tbl_Usuarios WHERE COD_IDENT_USUAR ='".$COD_IDENT_USUAR."'");

    //fechando a conexao com o banco
    mysql_close($conn);

?>

Using these codes, I am therefore not deleting, it sends the delete pageUsuario.php but nothing happens.

  • You didn’t ask a question like that yesterday ?

  • No, this is a new mistake because my other post has been disabled.

  • Transform the btn_Excluir in id and put the script at the end of the page, if you want to put at the beginning use the Jquery ready function.

2 answers

0


(the change button points to the same page as the delete button, I guess that’s not what you want!)

You are creating two, nay one row in table:

echo "<tr data-id='$linha[4]'>";
echo "<tr>";

The $trPai refers to the second <tr>, who doesn’t have the data-id; if you take that line the problem must be solved.

0

Check that the id on tr is being sent even by ajax, if it is not being sent try changing your javascript. If you have . btn_Excluir inside tr, use it as selector and to catch the id of tr through . Closest().

$(function(){
    $(".btn_Excluir").on("click", function(){
        var id = $(this).closest("tr").attr("data-id");
        if(id != null){
           //Ajax aqui
        } 
    });
});

Browser other questions tagged

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