如何在php while循环中的特定div中追加数据

我有一个评论系统,其中我附加了用户所做的评论,但是当我附加数据时,它来了: [object HTMLCollection]: [object HTMLCollection]我也想知道如何在用户评论的特定post上附加此评论。

我的PHP代码:

 $query = "SELECT ph.ext, ph.likes,ph.desccription, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path FROM photos as ph inner join followers as fol on fol.user_id = ph.uid where fol.uid = '$id' ORDER BY ph.image_url DESC limit 5"; $fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con)); if (mysqli_num_rows($fire)>0) { while ($users = mysqli_fetch_assoc($fire)) { $likes = $users['likes']; $description = $users['desccription']; $username = $users['username']; $uid = $users['uid']; $pixid = $users['id']; $avatar_path5 = $users['avatar_path']; ?> 
<img src="https://stackoverflow.com/questions/52889526/how-to-append-data-in-a-specific-div-in-a-php-while-loop/" width="100%" class="avatar">

<?php echo "

"; ?>

<img src="https://stackoverflow.com/questions/52889526/how-to-append-data-in-a-specific-div-in-a-php-while-loop/" alt="Avatar" style="width:100%;">
<input type="hidden" name="id" id="id" class="id" value=""> <input type="hidden" name="username" id="username" value=""> <input type="hidden" name="uid" id="uid" value="">
<div id="comments" class="comments" data-post-id=""> 0) { while ($row = mysqli_fetch_assoc($results)) { $commentid = $row['id']; $comment = $row['comment']; $string = covertHashtags($comment); echo "

"; echo "".$row['username'].""; echo " ".$string; $sql3 = "SELECT * FROM comment where id ='$commentid' and user_id='$id' order by comment desc limit 5 "; $results3 = mysqli_query($con,$sql3); if (mysqli_num_rows($results3)>0) { echo "

"; } else{ echo ""; } echo "

"; } }else{ echo ""; } ?>


和我的JavaScript

  $(document).on('click','.submit',function(e){ e.preventDefault(); //Get values of input fields from DOM structure var dataString = { id: $(this).parent().find("#id").val(), username: $(this).parent().find("#username").val(), uid: $(this).parent().find("#uid").val(), comment: $(this).parent().find("#comment").val() }; $.ajax({ url:'comments.php', data:dataString, success:function(){ $('#comments').append(''+username+': '+comment); } }); });  

我究竟做错了什么 ? 我已经更新了我的代码,并添加了整个PHP代码,提前谢谢。

您的用户名和评论变量尚未创建。

你喜欢这样:

 $(document).on('click','.submit',function(e){ e.preventDefault(); var post_id = $(this).attr("data-post-id"); //Get values of input fields from DOM structure var dataString = { id: $(this).parent().find("#id").val(), username: $(this).parent().find("#username").val(), uid: $(this).parent().find("#uid").val(), comment: $(this).parent().find(".comment").val() }; $.ajax({ url:'comments.php', data:dataString, success:function(){ $('#comments_'+post_id).append(''+$(this).parent().find("#username").val()+': '+$(this).parent().find(".comment").val() ); } }); }); 

编辑你的PHP脚本特别是在这一行,我刚刚添加了一个属性:

 `` 

这条线也是:

 

我还更新了$('#comments_'+post_id).append(''+$(this).parent().find("#username").val()+': '+$(this).parent().find(".comment").val()

我在js中添加了这个:

 var post_id = $(this).attr("data-post-id"); 

我不清楚你到底想要什么,但从我在你的代码中看到的内容我认为以下内容可能有所帮助。

(你也应该避免使用jQuery .on函数,而是使用.on

 $('.submit', document).click(,function(e){ e.preventDefault(); //Get values of input fields from DOM structure id = $(this).parent().find("#id").val(), username = $(this).parent().find("#username").val(), uid = $(this).parent().find("#uid").val(), comment = $(this).parent().find("#comment").val() var dataString = { id: id, username: username, uid: uid, comment: comment }; $.ajax({ url:'comments.php', data:dataString, success:function(){ $('#comments').append(''+username+': '+comment); } });