doukanxi4246
2017-09-04 15:16
浏览 44

PHP和AJAX删除仅适用于第一行

I made a simple PHP blog system. Under the Add Post page user can give a title and body text. The posts are on the Posts page, where the user can delete the posts.

My problem is: Only the first post can be deleted. The others not.

Here is my code for show the posts:

<?php
$database->query('SELECT * FROM posts');  
$rows = $database->resultSet();
?>

<div class="container">
 <?php foreach ($rows as $row) : ?>
 <h2><?php echo $row['title']; ?></h2>
 <p><?php echo $row['body']; ?></p>
 <small><?php echo $row['create_date'] ?></small>
 <form method="post">
  <input  id="deleteid" type="hidden" name="delete_id" value="<?php echo 
  $row['id'] ?>">
  <input id="delete_button" class="btn btn-primary" type="submit" 
   name="delete" value="Delete">
 </form>
 <?php endforeach; ?>
 <br><br>
</div>

And this is my AJAX code:

$("#delete_button").click(function () {
    $.ajax({
      type: "POST",
      url: "?action=delete",
      data: "delete_id=" + $("#deleteid").val(),
      success: function (result) {
        if (result == 1) {
          //window.location.assign("views/posts.php");
        } else {
          alert("Error");
        }
      }
    });
  });

图片转代码服务由CSDN问答提供 功能建议

我制作了一个简单的PHP博客系统。 在“添加帖子”页面下,用户可以给出标题和正文。 帖子位于帖子页面上,用户可以在其中删除帖子。

我的问题是:只能删除第一篇帖子。 其他没有。

以下是我展示帖子的代码:

 &lt;?php 
 $ database-&gt; query('SELECT * 来自帖子');  
 $ rows = $ database-&gt; resultSet(); 
?&gt; 
 
&lt; div class =“container”&gt; 
&lt;?php foreach($ rows as $ row):?&gt;  
&lt; h2&gt;&lt;?php echo $ row ['title'];  ?&gt;&lt; / h2&gt; 
&lt; p&gt;&lt;?php echo $ row ['body'];  ?&gt;&lt; / p&gt; 
&lt; small&gt;&lt;?php echo $ row ['create_date']?&gt;&lt; / small&gt; 
&lt; form method =“post”&gt; 
&lt;  input id =“deleteid”type =“hidden”name =“delete_id”value =“&lt;?php echo 
 $ row ['id']?&gt;”&gt; 
&lt; input id =“delete_button”class  =“btn btn-primary”type =“submit”
 name =“delete”value =“Delete”&gt; 
&lt; / form&gt; 
&lt;?php endforeach;  ?&gt; 
&lt; br&gt;&lt; br&gt; 
&lt; / div&gt; 
   
 
 

这是我的AJAX代码: \ n

  $(“#delete_button”)。click(function(){
 $ .ajax({
 type:“POST”,
 url:“?action = delete”,
 data  :“delete_id =”+ $(“#deleteid”)。val(),
 success:function(result){
 if(result == 1){
 //window.location.assign("views/  posts.php“); 
} else {
 alert(”Error“); 
} 
} 
}); 
}); 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtan7639 2017-09-04 15:25
    已采纳

    You cannot use same id for multiple input/div or any other HTML tags. Please change id="deleteid" to class="deleteid" and also change in your click event. So your this line will change to -

    <input  class="deleteid" type="hidden" data-deleteid="<?php echo 
      $row['id'] ?>" name="delete_id" value="<?php echo $row['id'] ?>">
    

    And you ajax code will change to -

    $(".delete_button").click(function () {
        var deleteid = $(this).data('deleteid');
        $.ajax({
          type: "POST",
          url: "?action=delete",
          data: "delete_id=" + deleteid ,
          success: function (result) {
            if (result == 1) {
              //window.location.assign("views/posts.php");
            } else {
              alert("Error");
            }
          }
        });
      });
    
    点赞 评论

相关推荐 更多相似问题