Reputation: 261
This is some part of my code, I'm using an ajax post and delete data, everything works fine, except that the data are not being deleted from my database. What am I doing wrong? here we go:
File view.php
<table class='uk-table uk-table-striped'><tbody>
<thead>
<tr>
<th>Ano</th>
<th>Grau</th>
<th>Serie</th>
<th>Curso</th>
<th>Instituição</th>
<th>Cidade</th>
<th>Estado</th>
<th>Excluir?</th>
</tr>
</thead>
<script type="text/javascript">
$(document).ready(function() {
$('#load').hide();
});
$(function() {
$(".delete").click(function() {
if (confirm("Tem certeza?"))
{
var row = $(this).parents('tr:first');
var id = $(this).attr("id");
var data = 'id=' + id ;
$.ajax({
type: "post",
url: "delete.php",
data: data,
cache: false,
success: function(){
row.slideUp('slow', function() {$(row).remove();});
}
});
}
return false;
});
});
echo "<tr id='".$historico['id']."'>";
echo "<td>".$historico['ano']."</td>";
echo "<td>".$grau['grau']."</td>";
echo "<td>".$serie['serie']."</td>";
echo "<td>".$curso['curso']."</td>";
echo "<td><a href='index.php?option=com_community&view=groups&task=viewgroup&groupid=".$grupo['id']."'>".$grupo['name']."</a></td>";
echo "<td>".$cidade['nome']."</td>";
echo "<td>".$estado['sigla']."</td>";
echo "<td><button class='delete uk-button uk-button-danger'>Delete</button></td>";
echo "</tr>";</tbody></table>
file delete.php
<?php
include 'con.php';
$id= $_POST["id"];
$query=mysql_query("DELETE FROM historico WHERE id = '$id'")or die(mysql_error());
?>
Upvotes: 3
Views: 2368
Reputation: 13534
Make things more simpler print the id in each delete button.
echo "<td><button class='delete uk-button uk-button-danger' id='".$historico['id']."'>Delete</button></td>";
And then use your code as it is.
As Andy G said You have to remove or modify the id attribute value of your tr
Upvotes: 0
Reputation: 19367
var id = $(this).attr("id");
this is attempting to read the id of the button that was clicked. You need the id for the tr:
var id = row.attr("id");
You don't need :first
either, you just want the tr
that contains the button:
var row = $(this).parents('tr');
parent('tr')
should work, rather than parents
. (parents probably works but is unnecessary.)
Upvotes: 2