简体   繁体   中英

Delete Data from MySQL Using PHP & AJAX

I have a one form and which have multiple form fields with remove button.All data is coming from DB.now when i am click on remove button i want to remove this data from DB using ajax.

PHP Code:-

<?php 
    for($i=0;$i<$hdntotal;$i++)
    { 
?>
           <div class="expform" id="<?=$i;?>">
           <label class="explabel">Expert Name: </label><input type="text" id="txtexpname" name="txtexpname[]" class="expfield" placeholder="Enter name of expert" value="<?=$expert_name[$i]; ?>"/>
           <div class="clearboth"></div>
           <label class="explabel">Link of the expert work: </label><input type="text" id="txtexplink" name="txtexplink[]" class="expfield" placeholder="Enter link of expert work" value="<?=$expert_user_links[$i];?>"/>
           <div class="clearboth"></div>
           <label class="explabel">Title of the review: </label><input type="text" id="txtreviewtitle" name="txtreviewtitle[]" class="expfield" placeholder="Enter title of review" value="<?=$expert_title[$i];?>"/>
           <div class="clearboth"></div>
           <label class="explabel">Details of the review: </label>
               <textarea id="txtrevdetails" name="txtrevdetails[]" class="expfield" placeholder="Enter details of review"><?=$expert_details[$i]; ?></textarea>
               <div class="clearboth"></div>
               <input type="hidden" value="<?=$rev_id[$i];?>" name="oldexprevids[]" >
               <input type="button" class="delReview" id="<?=$rev_id[$i];?>" value="Remove">
               <div class="line"></div>
           </div>
<?php 
    }   
?>

JS Code:-

/*AJAX Function for Delete Expert Review*/
jQuery(document).ready(function($) {

    $('button.delReview').each(function(){

        var $this = $(this);

        $this.click(function(){

            var deleteItem  = $this.attr('id');

            $.ajax({url:'delete-expreview.php?action='+deleteItem}).done(function(data){

                //colect data from response or custom code when success

            });
         return false;  
        });

    });
});
/*End fo AJAX Function*/

Delete Page Code:-

    $id = $_REQUEST['action'];
$sql5 = "delete from cc_tbl_car_review where id = '$id'";
$result5 = mysqli_query($db,$sql5) or die('Fetch Error');

But i am not getting any result.if you have any solution then please share it with me.

Pass Unique Id to ajax function . Onclick function should get unique Id for deletion. and Ajax Code Would be like this

   $('.delReview').click(function(){
        var id =$(this).attr('id');
        $.ajax({
         type: "POST",
 //path to delete php page
        url:"pathto/delete.php",
        data: "id="+id,
        success:function(result){

          //here is your success action
          //for refreshing page use this  
            $("#result1").html(result);
        });
});

And in Your delete.php get unique id like $_POST['id'] and perform action.

The Problem is with your ajax call. we have to pass data in name: value paai like

your ajax call will be look like this

$.ajax({
  url:'delete-expreview.php',
  data:{action:deleteItem},
  success:function(result){alert("message");}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM