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

如何使用javascript对话框从数据库中删除帖子?,javascript,php,post,dialog,Javascript,Php,Post,Dialog,我想创建一个对话框,这样用户可以确认他的操作,如果他想删除一篇文章。我现在遇到的唯一问题是,如果我确认该对话框,带有下一个ArticleID的帖子将被删除,而不是我想要删除的特定帖子。我做错了什么,或者我必须添加什么来防止此错误 $result = $link->query('SELECT * FROM artikel ORDER BY artikelID DESC'); while($record = $result->fetch_array()){ echo '<div

我想创建一个对话框,这样用户可以确认他的操作,如果他想删除一篇文章。我现在遇到的唯一问题是,如果我确认该对话框,带有下一个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>';
}   

如上所述。jQuery应该有一个用于删除对话框的函数,每个删除“按钮”都应该有某种数据

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

每个按钮不需要重复的脚本。另外,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>

“height=“15”width=“15”>

jQuery(函数($){ $(“img.popup”)。单击(函数(事件){ var id=$(this.attr('data-artikel-id'); $(“#对话框确认”)。对话框({ 可调整大小:false, 身高:30, 莫代尔:是的, 按钮:{ “blabla”:函数(){ location.href=“?actie=verwijderen&artikelID=“+id; $(此).dialog(“关闭”); }, 取消:函数(){ $(此).dialog(“关闭”); } } }); }); });
你粘贴的东西可能不是麻烦制造者,除非你的DB类中发生了一些奇怪的事情。同样,我们想看看你是如何处理删除
artikleID
的调用的。这是因为jquery将选择在dom
id=“popup>中找到的第一个id“
,请记住,每个文档只能有一个id。我认为问题在于,您正在为每个记录创建一个函数,但它们都由同一个对象调用,因此我假设最后一个将覆盖以前的所有记录。您可能只需要添加某种类型的
数据artikelid='
,然后在打开对话框时使用
$(this.data('artikelid')
$('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" );
             }
          }
      });
    });
 });
<?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>