我有脚本使用AJAX向DB添加注释,而不是在之前的注释结束时将其打印在屏幕上
如何更改它以便新评论(用户刚刚添加的评论)会出现在评论的顶部?
$(function() {
$(".addComment").click(function(){
var element = $(this);
var refID = element.attr("id");
var userID = element.attr("userID");
var main = $("#main:checked").val();
var contentText = $("#contentText").val();
var act = 'add';
var info = 'refID=' + refID + '&userID=' + userID + '&main=' + main + '&contentText=' + contentText + '&act=' + act;
// alert(info);
$("#loading").html('<img src="loader.gif" align="absmiddle"> loading...');
$.ajax({
type: "POST",
url: "ajax/comments.php",
data: info,
success: function(response){
$("#loading").ajaxComplete(function()
{
}).slideUp();
document.getElementById("contentText").value="";
$("#comments").append(response);
// $('.formComment'+ownerID).fadeOut(200).hide();
// $('.formComment'+ownerID).fadeIn(200).show();
}
});
return false;
});
});
<ul id="comments">
<?PHP
$query = mysql_query("SELECT * FROM `comments` WHERE (userID = '$_SESSION[userID]') ORDER BY date DESC");
while($index = mysql_fetch_array($query))
{
echo '<li id="comment_'.$index["id"].'">';
echo '<a href="#" class="del_button" id="del-'.$index["id"].'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a>';
echo $index["text"].'</li>';
}
?>
</ul>
PHP页面:
if(mysql_query("INSERT INTO comments (userID, pageName, refID, text, main, date) VALUES ('$_POST[userID]', 'yard', '$_POST[refID]', '$contentToSave', '$_POST[main]', '$dateLX') ") )
{
//Record was successfully inserted, respond result back to index page
$commentID = mysql_insert_id(); //Get ID of last inserted row from MySQL
echo '<li id="comment_'.$commentID.'">';
echo '<a href="#" class="del_button" id="del-'.$commentID.'">';
echo '<img src="../images/deleteS.png" border="0" />';
echo '</a>';
echo $contentToSave.'</li>';
// mysql_close($connecDB); //close db connection
}
答案 0 :(得分:4)
更改此行:
$("#comments").append(response);
要:
$("#comments").prepend(response);