Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用jquery将ajax响应附加到循环中的特定ul_Php_Jquery_Ajax - Fatal编程技术网

Php 如何使用jquery将ajax响应附加到循环中的特定ul

Php 如何使用jquery将ajax响应附加到循环中的特定ul,php,jquery,ajax,Php,Jquery,Ajax,伙计们,我已经做了一个星期了。我有一个由许多帖子组成的新闻提要。当用户对特定帖子发表评论时。我想在不刷新页面的情况下,将他们的评论附加到他们评论的特定帖子中。我有一些东西目前正在工作,但不正常 下面的代码每次用户发表评论时,都会在最后一篇文章中追加响应。如果页面上有100条帖子,用户评论在第一位。注释将附加在底部的100标记处 我使用postID作为我的唯一ID。哦,我的Javascript也在我的php循环中。任何人都可以帮忙 <ul class='post-comment-list

伙计们,我已经做了一个星期了。我有一个由许多帖子组成的新闻提要。当用户对特定帖子发表评论时。我想在不刷新页面的情况下,将他们的评论附加到他们评论的特定帖子中。我有一些东西目前正在工作,但不正常

下面的代码每次用户发表评论时,都会在最后一篇文章中追加响应。如果页面上有100条帖子,用户评论在第一位。注释将附加在底部的100标记处


我使用postID作为我的唯一ID。哦,我的Javascript也在我的php循环中。任何人都可以帮忙

 <ul class='post-comment-list we-comet' id='insertPostsComment$enCodePostsID'>
    <!--Post comment echo-->
        $getPostComments
    <!--Post comment echo-->
        <li>
            <a href='#' title='' class='showmore underline'>more comments</a>
        </li>
        <li id='commentForm$enCodePostsID' class='post-comment empty-comment-form'>
            <!--Comment form ajax out put-->
            <!--Comment form ajax out put-->
        </li>
 </ul>



 /*send comment to post comment reply controller with postID when button is clicked function*/
      function sendComment(e) {

      $('#submitCommentBtn').addClass('disabled');

        comment = $('textarea[name=commentField]').val();

      if(comment == ''){
          $('div#commentStatus').html('<small>comment is a required field *</small>').css('color', 'red').fadeIn();
          return false;
      }

      //get domain name only
      var urLorigin = window.location.origin;

      $.ajax({
          type: 'POST',
          url: urLorigin + '/appName/profile/ajaxSendComment',
          cache: false,
          data: $('form[name=comment-form]').serialize(),
          success: function (response) {

              $('textarea[name=commentField]').val(''); //empty textarea

              /*display successs message*/
              $('div#commentStatus').html('<small>Sent&nbsp;&#10003;</small>').css('color', 'green').fadeIn();

              /*apend comment after send*/
                $('div.coment-area').each(function(){
                    //append
                    $(response).clone().appendTo($(this).find('ul#insertPostsComment$enCodePostsID'));
                });
              /*apend comment after send*/

              /*delay message closing for 5 seconds*/
              $('div#commentStatus').delay(5000).fadeOut(0);

              $('#submitCommentBtn').removeClass('disabled');
          },
          error: function(){
              alert('An unknown error occurred!');
          }
      });

      }
    $getPostComments
/*单击按钮时,使用postID将评论发送给post评论回复控制器功能*/ 函数sendComment(e){ $('#submitCommentBtn').addClass('disabled'); comment=$('textarea[name=commentField]')。val(); 如果(注释=“”){ $('div#commentStatus').html('comment是必填字段*').css('color','red').fadeIn(); 返回false; } //仅获取域名 var urLorigin=window.location.origin; $.ajax({ 键入:“POST”, url:urLorigin+'/appName/profile/ajaxSendComment',, cache:false, 数据:$('form[name=comment form]')。序列化(), 成功:功能(响应){ $('textarea[name=commentField]).val(“”);//空textarea /*显示成功消息*/ $('div#commentStatus').html('Sent✓;')).css('color','green').fadeIn(); /*发送后发送评论*/ $('div.coment-area')。每个(函数(){ //附加 $(response.clone().appendTo($(this.find('ul#insertPostsComment$enCodePostsID')); }); /*发送后发送评论*/ /*延迟信息关闭5秒*/ $('div#commentStatus')。延迟(5000)。淡出(0); $('#submitCommentBtn').removeClass('disabled'); }, 错误:函数(){ 警报('发生未知错误!'); } }); }
“我有一些东西目前正在工作,但不正常。”-请详细说明“不正常”,以便在“我的Javascript在我的php循环中”之后解决确切的问题……这意味着您可能正在创建许多函数,所有这些函数都称为“sendComment”……您能理解为什么这可能会导致问题吗?我得到的印象是,虽然我们无法看到所有必要的HTML,也无法看到循环,但您也可能正在创建许多具有相同ID的HTML元素。考虑一下你如何想象浏览器能把它们分开,但这主要是猜测。我们需要了解HTML和JavaScript实际上是如何构造的。我们无法看到大部分JavaScript,因此缺少上下文。我们无法确定事情是如何执行的,或者会产生什么影响。请尝试创建一个循环,以便我们了解发生了什么。@Bhushan我在问题下面发布的代码,这就是我遇到的问题。@ADyson循环中的函数就是问题所在。我基本上创建了数百个函数,所以它是在最后一个函数的基础上进行迭代和附加。万分感谢!“我有一些东西目前正在工作,但不正常。”-你能详细说明一下“不正常”吗,这样确切的问题就可以解决了“我的Javascript在我的php循环中”…这意味着你可能正在创建很多函数,都称为“sendComment”…你能明白为什么这可能会导致问题吗?我得到的印象是,虽然我们无法看到所有必要的HTML,也无法看到循环,但您也可能正在创建许多具有相同ID的HTML元素。考虑一下你如何想象浏览器能把它们分开,但这主要是猜测。我们需要了解HTML和JavaScript实际上是如何构造的。我们看不到大部分JavaScript,因此缺少上下文。我们无法确定事情是如何执行的,或者会产生什么影响。请尝试创建一个循环,以便我们了解发生了什么。@Bhushan我在问题下面发布的代码,这就是我遇到的问题。@ADyson循环中的函数就是问题所在。我基本上创建了数百个函数,所以它是在最后一个函数的基础上进行迭代和附加。万分感谢!