2017-04-22 42 views
0

我想點擊刪除按鈕,我正在使用AJAX後刪除行的記錄,我嘗試以下,但它給我一個錯誤:刪除記錄在AJAX

//index.php 
$(document).on('click', '.delete', function(){ 
    var Serial = $(this).attr("Serial"); 
    if(confirm("Are you sure you want to remove this data?")) 
    { 
    var action = "Delete"; 
    $.ajax({ 
    url:"plans-action.php?square=$selsquare",  
    method:"POST",  
    data:{Serial:Serial, action:action}, 
    success:function(data) 
    { 
    fetchUser();  
    alert(data); 
    } 
    }) 
    } 
    else 
    { 
    return false; 
    } 
}); 
}); 

//plans-fetch.php 

這裏是我的在服務器端PHP代碼

if(isset($_POST["action"])) 
{ 

if($_POST["action"] == "Delete") 
{ 
    $statement = $connection->prepare(
    "DELETE FROM plans WHERE SquareNo = '$selsquare' AND Serial = :Serial" 
); 
    $result = $statement->execute(
    array(
    ':Serial' => $_POST["Serial"] 
    ) 
); 
    if(!empty($result)) 
    { 
    echo 'Data Deleted'; 
    } 
} 


} 
+0

data-serial="somevalue",而不是Serial="somevalue"

$(document).on('click', '.delete', function(e){ e.preventDefault(); // using data-serial // var serial = $(this).data('serial'); var serial = $(this).attr("Serial"); if(confirm("Are you sure you want to remove this data?")) { var action = "Delete"; var selsquare = <?php echo $selsquare; ?>; // echo value from php $.ajax({ url:"plans-action.php", // url:"plans-action.php?square=$selsquare" we don't need params method:"POST", data:{Serial:serial, action: action, selsquare: selsquare }, success:function(data) { fetchUser(); alert(data); } }) } else { return false; } }); 

然後你能告訴我們你的錯誤? –

+0

你會得到什麼錯誤? –

+0

您還需要從$ _GET獲取$ selsquare以傳遞查詢 –

回答

0

你不必爲你使用POST的參數追加到URL的結尾一樣。將參數包含在$.post函數的數據部分中。建議在使用自定義屬性時使用data-,你的序列號屬性應該是你PHP

if (isset($_POST["action"])) { 

    if ($_POST["action"] == "Delete") { 
     $selsquare = $_POST['selsquare']; 
     $statement = $connection->prepare("DELETE FROM plans WHERE SquareNo = '$selsquare' AND Serial = :Serial"); 
     $result = $statement->execute(array(':Serial' => $_POST["Serial"])); 
     if (!empty($result)) { 
      echo 'Data Deleted'; 
     } 
    } 

} 
+0

仍然不能正常工作 – martinBibo

+0

@martinBibo您是否收到任何錯誤? – julekgwa