2016-08-16 73 views
-1

我想點擊其ID的參考按鈕後刪除spacific行表的ID,刪除行使用AJAX

這裏是我的PHP代碼,

while($row = mysqli_fetch_array($values)) { 
    echo "<tbody>"; 
    echo "<tr id=\"12\">"; 
    echo "<td>" . $row['pid'] . "</td>"; 
    echo "<td>'<img src='/Login/product_avtar/".$row['pic']."' width=\"80\" height=\"60\">'</td>"; 
    echo "<td>" . $row['pname'] . "</td>"; 
    echo "<td>" . $row['pprice'] . "</td>"; 
    echo "<td>" . $row['pdes'] . "</td>"; 
    echo "<td>" . $row['qnt'] . "</td>"; 
    echo "<td><button class=\"btn btn-sm btn-danger delete_class\" id=\"".$row['pid']."\">Delete</button></td>"; 
    echo "</tr>"; 
    echo "</tbody>"; 

這是阿賈克斯,

$(function() { 
    $(".delete_class").click(function(){ 

    var element = $(this); 
    var del_id = element.attr("pid"); 
    var info = 'pid=' + del_id; 
    if(confirm("Are you sure you want to delete this Record?")){ 
     $.ajax({ 
      type: "POST", 
      url: ajax_url, 
      data: info, 

       success: function(){ 
       alert('Successfully Deleted'); 

      } 
     }); 
    } 
    return false; 
    }); 
}); 

,這是ajax_url,

if(($_POST['pid'])) 
{ 
    $id=$_POST['pid']; 
    $id = mysqli_real_escape_string($id); 

    $option = mysqli_query($conn,"DELETE * FROM product where pid = ".$id.""); 
} 

請幫忙找出解決方案, 感謝

回答

0

您已經使用屬性PID:

var del_id = element.attr("pid"); 

但在HTML中指定的屬性是ID,而不是PID。

更改此:

<button class=\"btn btn-sm btn-danger delete_class\" id=\"".$row['pid']."\">Delete</button> 

要這樣:

<button class=\"btn btn-sm btn-danger delete_class\" pid=\"".$row['pid']."\">Delete</button> 

並嘗試調試在您的jQuery:

var del_id = element.attr("pid"); 
console.log(del_id); // On chrome, go to the Inspect Element > console tab and check if the id retrieved is correct. 
+0

非常感謝你的快速回復,我已經改變ID到PID但它仍然不能正常工作, –

+0

您是否嘗試過console.log並檢查是否檢索了正確的值d點擊按鈕? –

+0

是的,我已經試過控制檯,但它並沒有顯示任何東西,它顯示網絡錯誤 –