How to delete a record from database through AJAX using php and also when link is clicked it returns to same page and new list is updated when a link is clicked and id passes through this link ? I am new to AJAX and php
3
-
follow this url very helpfull stackoverflow.com/questions/15917833/…Pankaj Upadhyay– Pankaj Upadhyay2015-07-10 05:37:58 +00:00Commented Jul 10, 2015 at 5:37
-
I have seen the question and its an answers,But it's not helping out.TOOR PIZZZ– TOOR PIZZZ2015-07-10 05:57:27 +00:00Commented Jul 10, 2015 at 5:57
-
post your code what are usingPankaj Upadhyay– Pankaj Upadhyay2015-07-10 06:05:04 +00:00Commented Jul 10, 2015 at 6:05
Add a comment
|
3 Answers
<a class="delete_data" id="<?php echo $row['id']; ?>">Delete</a>
ajax
$(document).ready(function(){
$(".delete_data").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'delete.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) { // Sucess
} else { // Error }
}
});
});
});
delete.php
$id = $_POST['delete_id'];
$query = "delete from TABLE NAME where ID = $id";
2 Comments
TOOR PIZZZ
Thanks! what does class in your link refer to <a class="delete_data" id="<?php echo $row['id']; ?>">Delete</a>
TOOR PIZZZ
it is deleting the record however I have to refresh the page to view the new list
@TOORPIZZZ You can add this to remove the row from the display without refreshing
$(document).ready(function(){
$(".delete_data").click(function(){
var del_id = $(this).attr('id');
var parent = $(this).parent();
$.ajax({
type:'POST',
url:'delete.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) { parent.slideUp(300,function() {
parent.remove();
} else { // Error }
}
});
});
});
Comments
Make sure you don't have } in a comment by //Error. It took a while until I discovered this.
$(document).ready(function(){
$(".delete_data").click(function(){
var del_id = $(this).attr('id');
var parent = $(this).parent();
$.ajax({
type:'POST',
url:'delete.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) { parent.slideUp(300,function() {
parent.remove();
} else { // Error } <----------------
}
});
});
});