2
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.
var id = parent.attr('COD_IDENT_USUAR');
shouldn’t bepai
instead ofparent
?– Sergio
made the changes but still goes only to the page excluiUsuario.php, however it does not forward the id.
– Renan Rodrigues
in PHP you are waiting to receive
$_POST['COD_IDENT_USUAR']
but in JS you are sendingdata: "id=" +id,
I think it should bedata: {id: id},
in the JS and$_POST['id']
in PHP...– Sergio
Notice: Undefined index: id in /opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/excluirUsuario.php on line 6
– Renan Rodrigues
Where the attribute element is in HTML
COD_IDENT_USUAR
?– Sergio
It comes from my query: // executes query and stores the returned result in the $result variable $result = mysql_query("SELECT COD_IDENT_USUAR, TXT_NOMEX_USUAR, TXT_ENDER_EMAIL, FLG_STATU_USUAR, DAT_ULTIM_LOGIN from tbl_USUARIOS");
– Renan Rodrigues
But where it is being inserted into HTML?
– Sergio