我希望能够单独获得每篇文章,但似乎我只获得第一篇。
是否没有办法我可以使用php唯一标识每个帖子,然后使用javascript访问相同的值?
或任何其他解决方案,如何处理有关不同帖子的多个评论。就像其他所有东西一样工作正常。
let otherCommentForms = document.querySelectorAll('.postComments');
otherCommentForms.forEach((otherComment) => {
otherComment.addEventListener('submit', (e)=>{
e.preventDefault();
// let post_id = $('#post_id_other').val();
let post_id = $(this).find('input#post_id_other').val();
// let userComment = $('#userCommentAreaOther').val();
let userComment = $(this).find('input#userCommentAreaOther').val();
// e.preventDefault();
console.log(`Post Identification: ${post_id} Comment: ${userComment}`);
console.log(`User Comment: ${userComment}`);
// $.post(serverScript, {
// comment: userComment,
// post_id: post_id
// }, function (data) {
// $("#theCommentsOther").html(data);
$(this).find('input#userCommentAreaOther').val('');
// });
});
});
这是 html代码:
<?php
$post_set = getPosts();
while ($post = mysqli_fetch_assoc($post_set)) {?>
<div class="central-meta item">
<div class="user-post">
<div class="friend-info">
<div class="friend-name">
<ins><a title=""><?php echo $post['headline']?></a></ins>
</div>
<div class="post-meta">
<img src="<?echo "PostImages/".$post['imageName']?>" alt="">
<div class="description">
<p><?php echo $post['caption'] ?></p>
</div>
</div>
</div>
<!-- friend-info -->
<ul class="we-comet">
<li class="post-comment">
<div class="post-comt-box">
<!-- comment form action to be removed -->
<form method="post" action="" class="postComments">
<input type="hidden" name="post_id" value="<?php echo $post['id']?>" id="post_id_other">
<!-- <textarea placeholder="Post your comment" name="comment" id="userComment" required></textarea> -->
<input type="text" name="comment" id="userCommentAreaOther" placeholder="Post your comment" class="form-control" required>
<input type="submit" value="Comment" class="mtr-btn signup" style="background:#4f93ce; color:white; border:#4f93ce;">
</form>
</div>
</li>
<div id="theCommentsOther">
<?php
$comment_set = find_comments($post['id']);
while ($comment_ind = mysqli_fetch_assoc($comment_set) ) {?>
<li style="display: inline-block; margin-bottom: 20px; width: 100%;">
<div class="we-comment">
<div class="coment-head">
<h5><?php echo userName($comment_ind['user_id']);?></h5>
</div>
<p><?php echo $comment_ind['content']?></p>
</div>
</li>
<?php }?>
</div>
<!-- where the comments are fed using ajax -->
</ul>
</div><!-- Userpost -->
</div><!-- central-meta -->
<?php } ?>
答案 0 :(得分:0)
对于每个要复制的表单ID,您都要进行循环检查。
答案 1 :(得分:0)
您要在HTML上循环使用相同的ID,请尝试使用唯一的ID