繁体   English   中英

使用PHP和AJAX从MySQL删除数据

[英]Delete Data from MySQL Using PHP & AJAX

我有一个表单,并有多个表单字段与删除按钮。所有数据来自DB.now当我点击删除按钮我想使用ajax从数据库中删除此数据。

PHP代码: -

<?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代码: -

/*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*/

删除页码: -

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

但是我没有得到任何结果。如果您有任何解决方案,请与我分享。

将唯一ID传递给ajax函数。 Onclick函数应该获取唯一ID以进行删除。 和Ajax代码就是这样的

   $('.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);
        });
});

在你的delete.php获取像$ _POST ['id']这样的唯一ID并执行操作。

问题在于您的ajax调用。 我们必须在名称中传递数据:值paai就像

你的ajax电话会是这样的

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

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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