【问题标题】:How to display comments for all post如何显示所有帖子的评论
【发布时间】:2019-04-12 12:10:16
【问题描述】:

我有一个帖子和评论系统。类似于 facebook 的帖子和评论系统。帖子显示正常,我可以对所有帖子发表评论。但是 cmets 仅显示第一个帖子,即显示在顶部的帖子。 . 我需要您帮助的问题如下: -每个帖子的cmets应该对应显示。

这些是我所做的。 查看:

  <div class="box box-widget">
                <div class="box-header with-border">
                  <div class="user-block">

                    <span class="description">Shared publicly - <?php echo time_ago($post['post_date'])?></span>

                  </div>
                </div>

                <div class="box-body" style="display: block;">
                  <img class="img-responsive show-in-modal" src="<?php echo base_url('post_file/'.$post['post_image'])?>" alt="">
                  <input type="hidden" id="stid" value="<?php echo $post['spid']; ?>">
                  <p><?php echo $post['postcontent']?></p>
                  <button type="button" class="btn btn-default btn-xs"><i class="fa fa-share"></i> Share</button>
                  <div>

                  <input type="hidden" id="pl_postid" name="pl_postid" value="<?php echo $post['spid']; ?>">

                  <button type="button" class="btn btn-default btn-xs"><i class="fa fa-thumbs-o-up"></i> Like</button>
                 </div>
                  <span class="pull-right text-muted"><?php //echo $countlikes ?> likes - 3 comments</span>

                </div>
                  <div id="display_comment"></div>
                <div class="box-footer" style="display: block;">
                    <form id="com" class="com" method="post">
                    <div class="img-push">
                        <input type="hidden"  class="status_id" id="status_id" name="status_id" value="<?php echo $post['spid']; ?>">

                        <textarea  name="comment" id="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>

                         <div class="box-footer box-form">
                          <btn class="btn btn-azure btn-sm pull-right commentbt" id="commentbt">Comment</btn>
                    <ul class="nav nav-pills">
                        <li><i class="fa fa-bullhorn"></i></li>

                    </ul>
                </div>
                    </div>
                  </form>
                    </div>

              </div>
              <?php endforeach;?>

jquery:

$(".commentbt").click(function(){
        var status_id = $(this).closest("div.img-push").find("input[name='status_id']").val();
        var comment = $(this).closest("div.img-push").find("textarea[name='comment']").val();
        alert(comment);

        var dataString = 'status_id='+ status_id +'&comment='+ comment;
        if(comment==''||status_id==''){
            alert('Can not send empty comment')
        }
        else{
            $('#display_comment').show();
            //$("#display_comment").fadeIn(100).html('<img src="<?php //echo base_url();?>uploads/ajax-loader.gif" />Loading Comment...');
            $.ajax({
               type:"POST",
               url:"<?php echo site_url('user/postcomment')?>",
               data:dataString,
               cache:false,
             success: function () {
         $(document).ready(function(){
        var status_id = $("#stid").val();
        $.post('<?php echo site_url('user/getcomments');?>',
        {
            status_id:status_id
        },
        function(data){
            $("#display_comment").html(data);
        });
    });


                    $('#com')[0].reset();
                }
            });
        }return false;
    });
});

谢谢

【问题讨论】:

  • cmets 的循环在哪里,通常需要一个循环来从数据库输出结果。再说一遍那些在哪里......你确定你首先从数据库中提取数据等等......
  • 是的,我用 foreach 做了,cmets 被发送到我使用 Jquery 显示的评论页面
  • 所以,第一个ajax 调用这个&lt;?php echo site_url('user/postcomment')?&gt; 工作正常,但下一个:$.post('&lt;?php echo site_url('user/getcomments');?&gt;' 应该得到提交的评论工作不正常?你说这个是在顶帖显示cmets?
  • $.post('' 正在工作,但只在顶部显示帖子的评论,忽略下面的其他帖子。
  • @SholleyonlineJava 我的回答对您有帮助吗?有时间请查看。

标签: php jquery ajax


【解决方案1】:

HTML 修复:

不要在不同的元素上使用相同的ID,就像使用display_comment 一样。您正在使用 foreach 循环创建一组帖子,因此这将创建具有相同 ID 的多个元素。请改用自定义类。 请记住具有相同ID 的项目不应超过一项。固定的元素是下一个:

&lt;input type="hidden" id="stid" value="&lt;?php echo $post['spid'];?&gt;"&gt;

&lt;input type="hidden" id="pl_postid" name="pl_postid" value="&lt;?php echo $post['spid']; ?&gt;"&gt;

&lt;div id="display_comment"&gt;&lt;/div&gt;

&lt;form id="com" class="com" method="post"&gt;

&lt;input type="hidden" class="status_id" id="status_id" name="status_id" value="&lt;?php echo $post['spid'];?&gt;"&gt;

&lt;textarea name="comment" id="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"&gt;&lt;/textarea&gt;

&lt;btn class="btn btn-azure btn-sm pull-right commentbt" id="commentbt"&gt;Comment&lt;/btn&gt;

由于您要创建多个box-widget,因此前面的元素将生成多个具有相同ID 的元素。固定的代码会是这样的:

<div class="box box-widget">

  <div class="box-header with-border">
    <div class="user-block">
      <span class="description">Shared publicly - <?php echo time_ago($post['post_date'])?></span>
    </div>
  </div>

  <div class="box-body" style="display:block;">
    <img class="img-responsive show-in-modal" src="<?php echo base_url('post_file/'.$post['post_image'])?>" alt="">
    <input type="hidden" class="stid" value="<?php echo $post['spid'];?>">
    <p><?php echo $post['postcontent']?></p>
    <button type="button" class="btn btn-default btn-xs">
      <i class="fa fa-share"></i> Share
    </button>
    <div>
      <input type="hidden" class="pl_postid" name="pl_postid" value="<?php echo $post['spid']; ?>">
      <button type="button" class="btn btn-default btn-xs">
        <i class="fa fa-thumbs-o-up"></i> Like
      </button>
    </div>
    <span class="pull-right text-muted"><?php //echo $countlikes ?> likes - 3 comments</span>
  </div>

  <div class="display_comment"></div>

  <div class="box-footer" style="display:block;">
    <form class="com" method="post">
      <div class="img-push">
        <input type="hidden" class="status_id" name="status_id" value="<?php echo $post['spid'];?>">
        <textarea name="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>

        <div class="box-footer box-form">
          <btn class="btn btn-azure btn-sm pull-right commentbt">Comment</btn>
          <ul class="nav nav-pills">
            <li><i class="fa fa-bullhorn"></i></li>
          </ul>
        </div>
      </div>
    </form>
  </div>

</div>

JQUERY 修复:

1) 本节的主要问题是您以ID 定位元素,并且您有多个具有相同ID 的元素,因此您可能只定位第一个带有出现在 HTML 上的 ID 的元素。那么,如何进行呢?尝试通过它们与单击按钮的关系来查找元素,就像您使用的方法一样:

var status_id = $(this).closest("div.img-push").find("input[name='status_id']").val();

2) 另一个问题是在 ajax 调用中使用 $(document).ready(),该问题已被删除。

一个固定的代码应该是这样的:

$(".commentbt").click(function()
{
    var status_id = $(this).closest("div.img-push").find("input[name='status_id']").val();
    var comment = $(this).closest("div.img-push").find("textarea[name='comment']").val();

    alert(comment);

    var dataString = 'status_id=' + status_id + ' &comment=' + comment;

    if (comment == '' || status_id == '')
    {
        alert('Can not send empty comment')

        // Finish function here if error detected!
        return;
    }

    // The next line was fixed!
    //$('#display_comment').show();
    var dComment = $(this).closest(".box-widget").find(".display_comment");
    dComment.show();

    // The next line was also wrong (Homework for you...)
    //$("#display_comment").fadeIn(100).html('<img src="<?php //echo base_url();?>uploads/ajax-loader.gif" />Loading Comment...');

    // Save clicked button object, we going to need it inside the ajax.
    var clickedBtn = $(this);

    $.ajax({
        type: "POST",
        url: "<?php echo site_url('user/postcomment')?>",
        data: dataString,
        cache: false,
        success: function()
        {
            // Wrong again, the next line was fixed!!
            //var status_id = $("#stid").val();
            var status_id = clickedBtn.closest(".box-widget").find("input.stid").val();

            console.log("Getting comments for status_id = " + status_id);

            $.post(
                "<?php echo site_url('user/getcomments');?>",
                {status_id: status_id},
                function(data)
                {
                   // Wrong again! Fixed!
                   //$("#display_comment").html(data);
                   dComment.html(data);
                }
            );

            // Another error, guess what...
            //$('#com')[0].reset();
            clickedBtn.closest("form.com").reset();
        }
    });
});

最后,我不得不说,我真的怀疑你的代码在 毕竟我做了所有的改变,但我希望至少你 了解您所犯的主要问题。

【讨论】:

  • 我应用了所有已识别的更改。仍然没有按预期工作,但在所有帖子上显示 cmets。显示的 cmets 是顶部帖子的 cmets。
  • 刚刚修复了这一行:var status_id = clickedBtn.closest(".box-widget").find("input.stid"); 到这个var status_id = clickedBtn.closest(".box-widget").find("input.stid").val();。此外,检查每个 .box.widget 元素的 &lt;?php echo $post['spid']; ?&gt; 是否不同。
猜你喜欢
  • 2020-02-18
  • 2012-05-02
  • 2013-03-14
  • 2017-02-11
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 2019-08-24
  • 2016-01-22
相关资源
最近更新 更多