php - display last comment at the top of the list -
i have script add comment db ajax , print on screen, @ end of previous comments
how can change new comment (the 1 user add) appear @ top of comments?
$(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 * `comments` (userid = '$_session[userid]') order 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 page:
if(mysql_query("insert comments (userid, pagename, refid, text, main, date) values ('$_post[userid]', 'yard', '$_post[refid]', '$contenttosave', '$_post[main]', '$datelx') ") ) { //record inserted, respond result index page $commentid = mysql_insert_id(); //get id of last inserted row 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 }
change line:
$("#comments").append(response); to:
$("#comments").prepend(response);
Comments
Post a Comment