2

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/… Commented Jul 10, 2015 at 5:37
  • I have seen the question and its an answers,But it's not helping out. Commented Jul 10, 2015 at 5:57
  • post your code what are using Commented Jul 10, 2015 at 6:05

3 Answers 3

5
<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";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! what does class in your link refer to <a class="delete_data" id="<?php echo $row['id']; ?>">Delete</a>
it is deleting the record however I have to refresh the page to view the new list
1

@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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.