試圖解決一個錯誤!我有一個阿賈克斯動力的社交網絡(與喜歡和評論等)。我有一個工作評論腳本,但是當我測試它時,我得到了原始帖子和評論的重複。每次輸出數據時,都會添加註釋並複製帖子和註釋,而不是僅添加評論。用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