簡體   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