试图解决一个错误!我有一个阿贾克斯动力的社交网络(与喜欢和评论等)。我有一个工作评论脚本,但是当我测试它时,我得到了原始帖子和评论的重复。每次输出数据时,都会添加注释并复制帖子和注释,而不是仅添加评论。用ajax驱动的社交网络复制mysql评论条目?
我已经使用jQuery的Ajax工作正常和PHP,可能是显而易见的,因为我新的jQuery和PHP我希望得到一些帮助?对不起,我的代码armount如果需要
感谢 jQuery的
$(".editable").live('click', function(){
$(this).empty();
$(this).mouseout(function() {
var comment = $(this).html();
var postid = $(this).attr("pid");
var commentuserid = $("#loggedin").attr("uid");
if(comment == ""){
return false;
}else{
//alert(comment);
//alert(postid);
//alert(commentuserid);
var datastring = 'comment=' + comment + '&postid=' + postid + '&commentuserid=' + commentuserid;
//save with ajax clear box and then load back in
$.ajax({
type: "POST",
url: "uploadcomment.php",
data: datastring,
success: function(data){
$(".usermsg").html(data);
}
});
}
});
});
PHP给人当评论上传
<?
$sql = "SELECT post.post, post.posttime, post.pid_imageurl, post.likes, user.name,
comments.comment, user.uid_imageurl, comments.comment_uid, post.pid
FROM post
INNER JOIN user
ON post.uid=user.uid
LEFT JOIN comments
ON comments.comment_pid=post.pid
ORDER BY pid DESC";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result)){?>
<?
$pid = $row['pid'];
$formattime = date("g:i:a",$row['posttime']);
echo '<p class="speechbubble">'.$row['post'].'</p>';
echo '<p class="msgholder" >Posted by '.$row['name'].' at '.$formattime. '<img class= "userprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/></p>';
?>
<div class="editable" pid="<? echo $row['pid'];?>" contentEditable="true">
add comment...
</div>
<?
$sql_comments = "SELECT comments.comment, comments.comment_time, user.name, user.uid_imageurl FROM comments
INNER JOIN user
ON comments.comment_uid = user.uid
WHERE comment_pid = '$pid' ORDER BY cid DESC";
$result_comments = mysql_query($sql_comments);
$numrows_comments=mysql_num_rows($result_comments);//num of rows
if($numrows_comments==0) //no comments
{
echo '<p>Comments</p><hr><br>';
echo 'This post has no comments yet, be the first!';
echo '<br><br><br><hr><br><br>';
}else{
echo '<p>Comments</p><hr><br>';
while($row=mysql_fetch_assoc($result_comments)){
$formattime_comment = date("g:i:a",$row['comment_time']);
echo '<p class="comment">'.$row['comment'].'</p>';
echo '<p class="commentposter">posted by '.$row['name']. ' at ' .$formattime_comment. '<img class="commentprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/></p>';
}//$sql_comments
echo '<hr><br><br>';
}//else end
}//$sql
?>
</div>
</div>
双岗我可以减少 - >
你没有显示你的更新代码或处理HTML更新的JS。另外,请用正确的<?php打开php,因为它只是<?是模棱两可的,可以解释为xml – 2012-01-04 19:45:20
更新代码工作正常我很确定它的这个或jquery,我刚刚添加如果你想看看它。我将在未来使用<?php,我也是这样使用的,但是大学的讲师并没有这么做,于是我停了下来。 – ChrisSherwood 2012-01-04 20:04:24
你会介意发布的HTML以及?我首先倾向于相信$(“。usermsg”)。html(data);应该改变另一个元素的html。你的ajax可能会返回整个html,并将其添加到内部元素而不是容器中。如果是这种情况,改变你的php只返回发布的评论,或者改变这个$(“。usermsg”)。html(data);到容器元件。在这种情况下,它不会重复,它只是将额外的html添加到加载的页面。 – 2012-01-04 20:11:40