Delete on the bank using ajax and jquery

2

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 02.06.2015 / 22:13

2 answers

4

Switch

<a href='excluirUsuario.php' id='btn_Alterar'><i class='glyphicon glyphicon-pencil'></i></a>

By

<a href='excluirUsuario.php' id='".$ID_DO_USER."' class='deleta'><i class='glyphicon glyphicon-pencil'></i></a>

Above you determine a class, because there will be multiple rows. And the user ID for each one in LINK. That's just a Pseudo-Code. Set your way, put the ID variable correctly in the ID="" field.

$(".deleta").click(function(e){
        e.preventDefault(); 
        var pai = $(this).closest('TR'); 
        var id = this.id; 
        $.ajax({ type: "POST", 
            data: "id=" +id, 
            url: "excluirUsuario.php", 
            success: function(msg){ $('#'+id).fadeOut(300); 
        } 
});

I did not even see your PHP.

    
02.06.2015 / 22:19
1

First, you have to put in your query the user ID, which by its delete code is COD_IDENT_USUAR :

 $resultado = mysql_query("SELECT TXT_NOMEX_USUAR, TXT_ENDER_EMAIL, FLG_STATU_USUAR, DAT_ULTIM_LOGIN, COD_IDENT_USUAR from tbl_USUARIOS");

Place the id read as data attribute on the line:

 while($linha = mysql_fetch_array($resultado))
 {
    echo "<tr data-id='$linha[4]'>";

There is a problem with the buttons you are inserting in the rows, you can not use the same id for everyone. Instead, use a class:

 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>";

Finally, read the attribute to send the parameter to the delete script:

$("#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(); 
            } 
    }); 
}); 

As you dynamically created your elements, you have to associate the click event with a parent element of them (in my example I used #elemento-pai-da-tabela , you did not show). I suggest leaving a fixed table and adding the rows dynamically.

    
02.06.2015 / 22:30