Deleting ajax + jquery

0

The correct method to delete, my question is how to get the id based on the table row, and how to send the same to a page that it actually excludes. The codes I have already done will be followed.

userlist.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);
?>

excludeUser.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 not consequently doing the delete, it sends the page deleteUsuario.php because nothing happens.

    
asked by anonymous 03.06.2015 / 15:02

2 answers

0

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

You are creating two , not a line in the table:

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

$trPai refers to the second <tr> , which does not have data-id ; if you take that line the problem should be solved.

    
03.06.2015 / 17:51
0

Make sure that the id that is in the tr is being sent even by ajax, if it is not being sent try changing your javascript. If you have the .btn_Exclude inside tr, use it as a selector and to get the tr id through .closest ().

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