如何使用ajax将数据附加到特定帖子?

时间:2013-10-02 17:38:40

标签: javascript jquery ajax

要点:

我有一个帖子列表,每个帖子还包含一个评论列表。我可以选择直接在帖子上添加评论(很像twitter)。我通过ajax提交这些帖子。

问题:

提交新评论时,会更新每个帖子的所有“评论列表”,而不仅仅是我提交的评论。

有什么想法吗? (以下代码)

JS:

$(document).ready(function () {
    var options = {
        //clearForm: true,
        //resetForm: true,
        //beforeSubmit: ShowRequest,
        success: function (html) {
            $('.post_comment_list').prepend(html);

            $('.footer-post').hide();
            $('.comments-feed').hide();
            $('.small-textarea-main-feed').removeClass('set-large');
            resetForm($('.footer-comment'));
        },
        error: function () {
            alert('ERROR: unable to upload files');
        },
        complete: function () {

        },
    };

    $(".footer-comment").ajaxForm(options);

    function ShowRequest(formData, jqForm, options) {
        var queryString = $.param(formData);
        alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
        return true;
    }

    function resetForm($form) {
        $form.find('input:text, input:password, input:file, select, textarea').val('');
        $form.find('input:radio, input:checkbox')
            .removeAttr('checked').removeAttr('selected');
    }
});

PHP

       <?php 
     if (empty($_POST) === false && empty($errors) === true) {
      //register user
         $post_comment = array(
        'comment'      => $_POST['comment'],
        'id'      => $_POST['id'],
         );


        $user_id = $_SESSION['user_id'];
        post_comment_db($user_id, $post_comment);
        //print_r($post_question['tags']);
        load_comment($user_id,$post_comment);



        } else{
          echo output_errors($errors);
         }
           ?>

PHP / HTML:Li(要添加的评论)

            function load_comment($user_id,$post_comment){


          $username = mysql_result(mysql_query("SELECT `username` FROM `users` WHERE `user_id` = $user_id"), 0, 'username');
          $timestamp = mysql_result(mysql_query("SELECT `timestamp` FROM `comments` WHERE `user_id` = $user_id"), 0, 'timestamp');
           $r = format_time($timestamp);

          $question_id = $post_comment['id'];

           $q = "SELECT `comment_id` FROM `question_has_comments` WHERE `question_id` = $question_id ORDER BY `timestamp` DESC LIMIT 1" ;
           $q = "SELECT `comment_id` FROM `comments` WHERE `question_id` = $question_id ORDER BY `timestamp` DESC LIMIT 1" ;


          echo 
           '                
    <li id="" class="post_comment">                                     
        <!--  wrapper da imagem -->
        <div id="" class="give-margin">
            <div id="" class="profile-page-avatar-wrapper"> 
                <a href="#"><img id="" class="profile-page-avatar-image" src="./images/test/chemistry.jpg" alt=""></a><!--  A imagem -->
            </div>
            <!--  o botao e o texto-->

            <div id="" class="profile-page-uploader-tools"> 
                <!--  o botao -->
                <div id="" class="profile-image-btn">
                    <div id="" class="profile-page-btn-wrapper">
                        <div id="" class="header-id">
                            <a href="#"><span id="user-name">' . $username . '</span></a>   
                        </div>
                        <div id="" class="question-page-feed-answer-header-timer">
                            <a id="feed-answer-header-timer" href="#"><span class="timer" data-time="">' . $r . '</span></a>
                        </div>
                    </div> <!-- fecha Div wrapper do botao-->
                </div> 
                <!--  fecha botao 
                http://www.w3.org/TR/html-markup/Overview.html#toc-->
                <p>' . $post_comment['comment'] . '</p>
            </div>                                  
        </div>
    </li>';

}

1 个答案:

答案 0 :(得分:1)

您的上一条评论回复确定了问题:

the class "post_coment_list" is an "ol" present in all posts, where the comments to a post reside

来自api:

.prepend(): Description:将参数指定的内容插入匹配元素集中每个元素的开头。 < / p>

在您的代码中,ajax success函数会在返回的HTML前面添加如下内容:

$('.post_comment_list').prepend(html);

由于$('.post_comment_list')是一组具有类.post_comment_list的所有元素,并且由于每个帖子都有该类,因此您的HTML将被添加到每个帖子中。

要解决此问题,请为每个帖子分配一个唯一ID,并在成功函数中,仅将HTML添加到该ID中。

要获取该ID,您可以在进行ajax调用时获取该ID,并且:

  1. 将ID分配给全局变量并在成功fn或
  2. 中再次抓取它
  3. 将ID与其他ajax数据一起发送,然后将其与HTML一起发送回成功fn。例如:
  4. PHP方面:

    $post_id = $_POST['postid'];
    
    $send_back = $post_id . '|' . '<li id="" class="post_comment">                                     
        <!--  wrapper da imagem -->
        <div id="" class="give-margin">
        etc
        ';
    echo $send_back
    

    jQuery / javascript:(内部成功:功能)

        var arrHTML = html.split('|');
        var postId = arrHTML[0];
        var html_code = arrHTML[1];
    
        $('#'+postId).prepend(html_code);
    

    请注意,上面,我没有演示将帖子ID发送到PHP端。我相信你没事。只是表明了解释我的建议。