Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting data from table using jQuery and SQL

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());




?>
like image 714
Thavia Farias Avatar asked Sep 20 '26 23:09

Thavia Farias


1 Answers

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.)

like image 98
Andy G Avatar answered Sep 23 '26 12:09

Andy G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!