概括:
我有一个帖子列表,每个帖子还包含一个评论列表。我可以选择直接在帖子上添加评论(很像 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:李(待补充的注释)
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>';
}