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

How can I delete my post from the database using a javascript dialog?

本文关键字:删除 我的 数据库 何使用 javascript 对话框      更新时间:2023-09-26

我想创建一个对话框,这样用户如果想删除帖子,就可以确认他的操作。我现在唯一的问题是,如果我确认对话框,带有下一个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'].';');

如上所述。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" );
             }
          }
      });
    });
 });

您不需要为每个按钮使用重复的脚本。此外,您的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>