การลบข้อมูลด้วย jquery และ ajax จะทำให้เราสะดวกในการลบที่รวดเร็วยิ่งขึ้น ดังตัวอย่างต่อไปนี้
<head>
<script>
$(function(){
$(".deleteItem").live("click",function(event){
event.preventDefault();
var ID = $(this).attr("item_id");
var dataString='action=delete&id=' + ID;
if(confirm("ต้องการจะลบข้อมูลนี้ใช่หรือไม่!")){
$(this).parent("td").parent("tr").fadeOut("slow");
$.ajax({
type: "POST",
url: "database_query.php",
data: dataString,
cache: false,
success: function(html)
{
}
});//สิ้นสุดการส่งข้อมูล
}
});
});
</script>
</head>
<body>
<table>
<tr>
<td><a href="#" item_id="ค่าที่จะส่งไปลบในฐานข้อมูล[1]" class="deleteItem">ลบ</a></td>
</tr>
<td><a href="#" item_id="ค่าที่จะส่งไปลบในฐานข้อมูล[2]" class="deleteItem">ลบ</a></td>
<tr>
<td><a href="#" item_id="ค่าที่จะส่งไปลบในฐานข้อมูล[3]" class="deleteItem">ลบ</a></td>
</tr>
</table>
</body>