如何使用javascript对话框从数据库中删除我的帖子?

时间:2013-07-03 14:54:33

标签: javascript php post dialog

我想创建一个对话框,以便用户在删除帖子时可以确认其操作。我现在唯一的问题是,如果我确认对话框,则删除包含下一个ArticleID的帖子而不是我想要删除的特定帖子。我做错了什么或者我需要添加什么来防止这个错误?

$result = $link->query('SELECT * FROM artikel ORDER BY artikelID DESC');
while($record = $result->fetch_array()){

 echo '<div id="dialog-confirm" title="Delete post?">
</div>';

echo '<div class="artikel" style="clear: both;">
    <a href="?actie=aanpassen&artikelID='.$record['artikelID'].'" id="aanpassenpost"><img src="icons/aanpassen.png" height="15" width="15"></a>
        <img src="icons/delete.png" id="popup" height="15" width="15">
            <script>
jQuery(function($) {
    $("img#popup").click(function(event) {
             $( "#dialog-confirm" ).dialog({
              resizable: false,
              height:30,
              modal: true,
              buttons: {
                  "blabla": function(){
                    location.href = "?actie=verwijderen&artikelID='.$record['artikelID'].'";
                      $( this ).dialog( "close" );
                   },
              Cancel: function() {
                 $( this ).dialog( "close" );
                 }
              }
          });
        });
     });
     </script>' ;
    echo'   <h3>'.$record['titel'].'</h3>
        <p>'.$record['inhoud'].'</p>
    </div>';
}   

这是删除帖子的代码:

if(isset($_GET['actie']) && $_GET['actie'] == "verwijderen"){
$link->query('DELETE FROM artikel WHERE artikelID='.$_GET['artikelID'].';');

2 个答案:

答案 0 :(得分:1)

如上所述。你的jQuery应该有一个删除对话框的功能,每个删除'按钮'应该有一些数据。

echo '<img src="icons/delete.png" class="popup" height="15" width="15" data-artikelID=' . $record['artikelID'] . ' />'

然后是jQuery函数

$('img.popup').click(function(event) {
         $( "#dialog-confirm" ).dialog({
          resizable: false,
          height:30,
          modal: true,
          buttons: {
              "blabla": function() {
                  location.href = "?actie=verwijderen&artikelID=" + $(this).data('artikelID');
                  $( this ).dialog( "close" );
               },
          Cancel: function() {
             $( this ).dialog( "close" );
             }
          }
      });
    });
 });

答案 1 :(得分:1)

每个按钮不需要重复的脚本。您的jQuery选择器也是相同的,因此单击按钮的方式与预期无关。尝试这样的事情:

我们在这里做的是在id的链接上设置数据属性,然后在jquery脚本中检索它并在重定向链接中使用它。

<?php

$result = $link->query('SELECT * FROM artikel ORDER BY artikelID DESC');

while ($record = $result->fetch_array()) : 

?>

<div class="artikel" style="clear: both;">
    <a href="?actie=aanpassen&artikelID=<?php echo $record['artikelID'] ?>" id="aanpassenpost">
        <img src="icons/aanpassen.png" height="15" width="15">
    </a>
    <img src="icons/delete.png" class="popup" data-artikel-id="<?php echo $record['artikelID'] ?>" height="15" width="15">
    <h3><?php echo $record['titel'] ?></h3>
    <p><?php echo $record['inhoud'] ?></p>
</div>

<?php endwhile; ?>   


<div id="dialog-confirm" title="Delete post?"></div>


<script>
jQuery(function($) {
    $("img.popup").click(function(event) {
        var id = $(this).attr('data-artikel-id');

        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:30,
            modal: true,
            buttons: {
                "blabla": function(){
                    location.href = "?actie=verwijderen&artikelID=" + id;
                    $(this).dialog( "close" );
               },
               Cancel: function() {
                   $( this ).dialog( "close" );
               }
           }
       });
    });
 });
 </script>