2013-09-22 27 views
-1

我想刪除file.My代碼沒有確認,這就像確認消息之前刪除

部分
<form action="delete.php" method="get"> 
<table> 
<?php 
    foreach ($files as $file) { 
        ?> 
<tr> 
     <td><?php echo $fajl['file_name'];?></td> 
     <td><a href="delete.php?id=<?php echo $file['file_id'];?>"><img src="img/delete.png"/></a></td> 
    </tr> 
     <?php } ?> 
     </table> 
    </form> 

,該delete.php

...if(isset($_GET['id'])){ 
    $id = $_GET['id']; 
    $query = $pdo->prepare('DELETE FROM files WHERE file_id = ?'); 
    $query->bindValue(1, $id); 
    $query->execute(); 
} 
$files = $file->fetch_all(); 
... 

之前必須有一個確認當我點擊刪除圖像文件被刪除。它工作的很好,但我想在刪除文件之前進行確認。我試着用斑馬對話框做到這一點我想補充類=「刪除」

<a href="delete.php?id=<?php echo $file['file_id'];?>"><img class="delete" src="img/delete.png"/></a> 

和下一個代碼

<script> 
    $(document).ready(function() { 
$(".delete").bind("click", function (e) { 
    e.preventDefault(); 
    var $this = $(this); 
    $.Zebra_Dialog("Do you want to delete this file?", { 
     type: "question", 
     title: "Are you sure?", 
     buttons: ['Yes', 'No'], 
     onClose: function (caption) { 
      if (caption == 'Yes') { 
       $.ajax({ 
        url: 'delete.php', 
        data: { 
         id: $this.data('id'), 
         success: function(data){ 
      alert("File deleted"); 
     } 
        } 
       }) 
      } 
     } 
    }) 
}); 
}); 
    </script> 

,但它不工作... 附: 現在我看到斑馬對話框有一個選項

'buttons': [ 
       {caption: 'Yes', callback: function() { alert('"Yes" was clicked')}}, 
       {caption: 'No'}, 
      ] 

難道不是

callback: function() { alert('"Yes" was clicked')} 

我打電話

delete.php?id=<?php echo $file['file_id'] 
+1

你不應該刪除的東西在響應GET請求。使用表單。 – SLaks

回答

0

您可以使用它返回true功能confirm('Message here')false

$(document).ready(function() { 
    $(".delete").bind("click", function (e) { 
     e.preventDefault(); 
     if (window.confirm('Are you sure?')) 
     { 
      // .. ajax call .. 
     } 
    }); 
}); 

編輯:

你有你的Ajax調用內部的語法錯誤。

$.ajax({ 
    url: 'delete.php', 
    data: { id: $this.data('id') }, 
    success: function(data){ 
     alert("File deleted"); 
    } 
}); 

此外,沒有$this.data('id'),所以你的HTML裏面:

<a data-id="<?= $file['file_id'] ?>" class="delete"><img src="img/delete.png"/></a> 
+0

確認和警報都是非常醜陋的對話框。大多數人傾向於避免他們,因爲他們可怕的外觀。 – Mark

+0

@Marcel:是的,但是他們是功能性的和工作的,即使他們看起來並不漂亮。 –

+0

他們不看*在Firefox中*不好(並且不要阻止所有的窗口/標籤)......(即他們看起來並不比其他模式對話更糟糕) – MvanGeest