2012-08-26 36 views
0

到目前爲止,我的腳本中的一切似乎都很完美。但是通過Json插入和檢索的數據沒有被插入到我的div中。任何敏銳的眼睛都能看到任何問題。阿賈克斯不張貼到div

<div id="commentaddid"></div> 

AJAX

<script> 
    $(document).ready(function(){ 
    $("form#mycommentform").submit(function(event) { 
    event.preventDefault(); 
    var streamidcontent = $(this).children('#streamidcontent').val(); 
    var content = $(this).children('#content').val(); 

    $.ajax({ 
    type: "POST", 
    url: "comment_add.php", 
    cache: false, 
    dataType: "json", 
    data: { streamidcontent: streamidcontent, content: content}, 
    success: function(data){ 
    $('#commentaddid').append('<div class="stream_comment">"+content+"</div>'); 
    } 
    }); 
    return false 
    }); 
    }); 
    </script> 

MAIN STATUS AJAX我從工作。

<script> 
$(document).ready(function(){ 
$("form#myform").submit(function(event) { 
event.preventDefault(); 
var content = $("#toid").val(); 
var newmsg = $("#newmsg").val(); 

$.ajax({ 
type: "POST", 
url: "insert.php", 
cache: false, 
dataType: "json", 
data: { toid: content, newmsg: newmsg }, 
success: function(response){ 
$("#newmsg").val(""); 
$("#homestatusid").html("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div class'delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr>"+newmsg+"<div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');clearTimeout(streamloop);swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'><a title='See who likes "+response['first']+" "+ response['middle']+" "+response['last']+"s status' href='include/likes.php?streamitem_id="+response['streamitem_id']+"' /></a></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'><table width=100%><tr><td valign=top width=30px><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><td valign=top align=left><div class='stream_comment_inputarea'><input id='addcomment' type='text' name='content' style='width:100%;' class='input_comment' placeholder='Write a comment...' onkeyup='growcommentinput(this);' autocomplete='off' onkeypress=\"if(event.keyCode==13){addcomment("+response['streamitem_id']+",this.value,'comment_list_"+response['streamitem_id']+"',"+response['id']+",'"+response['first']+" "+ response['middle']+" "+response['last']+"');this.value='';}\"><br/></div></div>"); 
} 
}); 
return false 
}); 
}); 
</script> 
+0

它通常是* *一個PHP *或*一個JavaScript問題。請再次驗證PHP部分是否正常工作,因此您不必將其發佈。 –

+0

...但是,請發佈PHP腳本生成的示例JSON。這在調試某些問題時很有用。 – LSerni

回答

2

(注:也許你不想.appenddiv但更換div的內容)。

這段代碼是錯的 - 你不匹配的單引號和雙引號:

$('#commentaddid').append('<div class="stream_comment">"+content+"</div>'); 

應該是:

$('#commentaddid').append('<div class="stream_comment">'+content+'</div>'); 

...實際上,應該不止於此。

data由JSON返回的值是一個複雜的對象,你應該格式(這將是審慎的PHP發出對JSON一個Content-Type太):

內容=「」 +數據。名稱+「評論:」+ ...

2

您獲取數據的data,但要附加作爲content(這實際上是要發送的數據)。更改您的代碼

$('#commentaddid').append('<div class="stream_comment">'+data+'</div>'); 
+0

順便說一句,你確定你想要追加原始的JSON嗎,還是應該繞過它,比如在添加JSON數據之前創建一些HTML元素? – SexyBeast

+0

它的用戶評論,添加到主帖子。評論應該向上並顯示輸入區域上方的最後評論。非這些答案正在工作。我已經嘗試了其中的大部分。 – dave

+0

它不應該工作。您只是試圖將JSON數據附加到'div',而實際上,您應該從JSON數組中獲取數據,構建HTML元素,如'

'+ data.first +'

'',然後追加它。 – SexyBeast

0
$('#commentaddid').append('<div class="stream_comment">'+data.content+'</div>');