2013-09-22 87 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