我有以下代码删除了一个mysql记录,我想将其转换为一个ajax / jquery代码,所以我可以在从表中删除记录后保持在同一页面。 现在,它工作正常,但它不在同一页面上,我需要刷新页面才能看到结果。这只是完整代码的一部分。 所有代码都在一页上。
//get the mysql results
//this is a repeated region
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="manage.php?id=<?php echo $row['id']; ?>">Delete</a>
</td>
</tr>
if ((isset($_GET['deleteid'])) && ($_GET['deleteid'] != "") && (isset($_POST['delete']))) {
$deleteSQL = sprintf("DELETE FROM my_table WHERE id=%s",
GetSQLValueString($_GET['deleteid'], "int"));
答案 0 :(得分:10)
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><div class="delete_class" id="<?php echo $row['id']; ?>">Delete</div></td>
</tr>
现在在本节中写下这个
$(document).ready(function(){
$(".delete_class").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'delete_page.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) { // DO SOMETHING
} else { // DO SOMETHING }
}
});
});
});
现在在'delete_page.php'页面中执行此操作
$id = $_POST['delete_id'];
$query = "delete from TABLE NAME where ID = $id";
休息我希望你知道。希望这有帮助
答案 1 :(得分:0)
包含jQuery,类似这样的东西可以解决问题
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="#" onClick="delete (<?php echo $row['id']; ?>); return false;">Delete</a>
</td>
</tr>
<script>
function delete(id){
$.post("manage.php", { deleteid: id , delete: true} );
}
</script>