0

ユーザーが投稿を削除したい場合に自分のアクションを確認できるように、ダイアログを作成したいと思います。私が今抱えている唯一の問題は、ダイアログを確認すると、削除したい特定の投稿ではなく、次の 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'].';');
4

2 に答える 2

1

上記のように。jQuery には削除ダイアログ用の関数が 1 つ必要であり、各削除「ボタン」には何らかのデータが必要です。

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" );
             }
          }
      });
    });
 });
于 2013-07-03T15:04:50.780 に答える
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>
于 2013-07-03T15:04:54.247 に答える