所以我试图通过ajax从表中删除数据。我在网上找到了这个教程,但当我点击删除按钮时,它只是将我带到页面顶部而不是删除我的数据。它还会在控制台中抛出一个未定义错误的元素。感谢任何帮助
<?php
$sql = "SELECT id,title,location,date,fb_page FROM gigs order by id desc ";
$result = mysql_query($sql) or trigger_error(mysql_error() . $sql);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['location'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['fb_page'].'</td>';
echo '<td> <div><a href=gigpic.php?id=' . $row['id'] . ' class="group1"><img class="img-responsive" src=gigpic.php?id=' . $row['id'] . ' height="50%" width="50%"></a></div> </td>';
?>
<td><a href="#" id="<?php echo $row['id']; ?>" class="delbutton"><i class="fa fa-trash-o fa-fw fa-3x"></i></a>
<div><a href="update_event.php?id=<?php echo $row['id'] ?>" class="iframe" ><i class="fa fa-refresh fa-fw fa-3x"></i></a></div></td>
<?php
echo '</tr>';
}
?>
我的Javascript
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = element.attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to delete this update? There is NO undo!")) {
$.ajax({
type : "POST",
url : "delete_event.php",
data : info,
success : function() {
}
});
$(this).parents(".record").animate({
backgroundColor : "#fbc7c7"
}, "fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});
</script>
答案 0 :(得分:0)
元素未定义正是它所说的。您有一个未定义的变量命名元素。
var del_id = element.attr("id");
您可能打算这样做:
var del_id = $(this).attr("id");