Ajax注释将json调用到函数中


Ajax comments calling json into function

我正在处理ajax注释,想知道如何使用下面的代码从comment_add.php页面中获取项目,以便将其插入obj.innerHTML.

function addcomment(streamid,content,containerid,posterid,postername,postid){
var obj = document.getElementById(containerid);
$.post("../comment_add.php", { streamid: streamid,content:content} );
obj.innerHTML = obj.innerHTML + "<div class='stream_comment'><table width='100%'><tbody><tr><td valign='top' width='30px'><img style='border:none;padding:0px;height:30px;width:30px;border-radius:0px;' src='imgs/cropped"+posterid+".jpg' onerror='this.src=&quot;img/no_profile_img.jpeg&quot;;'></td><td valign='top' align='left'><a href='profile.php?username="+posterid+"'>"+postername+" </a>"+content+"</td></tr></tbody></table></div>";

COMMENT_ADD.PHP

 <?php
    session_start();
    require"include/load.php";
    if(isset($_POST['streamid'])&isset($_POST['content'])){
    user_core::add_comment($_POST['streamid'],$_POST['content']);

    }
    $json = array();
    $check = "SELECT comment_id,comment_streamitem, comment_content FROM streamdata_comments";
    $check1 = mysqli_query($mysqli,$check);
    $resultArr = mysqli_fetch_array($check1);
    $json['comment_id'] = $resultArr['comment_id'];
    $json['comment_streamitem'] = $resultArr['comment_streamitem'];
    mysqli_free_result($check1);
    $check = "SELECT * FROM users WHERE id=".$_SESSION['id']."";
    $check1 = mysqli_query($mysqli,$check);
    $resultArr = mysqli_fetch_array($check1);
    $json['username'] = $resultArr['username'];
    $json['id'] = $resultArr['id'];
    $json['first'] = $resultArr['first'];
    $json['middle'] = $resultArr['middle'];
    $json['last'] = $resultArr['last'];
    mysqli_free_result($check1);
    echo json_encode($json);
    ?>

我确实试过使用这个代码。然而,每次我发表评论时,它都只会将其添加到流中的第一个帖子中,就像顶部函数没有的地方一样,它会将它添加到用户评论的状态中。

<script>
$(document).ready(function(){
$("form#mycommentform").submit(function(event) {
event.preventDefault();
var streamid = $("#streamid").val();
var content = $(this).children('#content').val();
$.ajax({
type: "POST",
url: "comment_add.php",
cache: false,
dataType: "json",
data: { 'streamid': streamid, 'content': content}, 
success: function(html){  
$("#commentaddid").html("<div class='stream_comment_holder' id='comment_holder_"+html['comment_streamitem']+"'><div id='comment_list_"+html['comment_streamitem']+"'><div class='stream_comment' id='comment_"+html['comment_id']+"'>div class='stream_comment_holder' id= style='display:;'><div class='stream_comment'><table width='100%'><tbody><tr><td valign='top' width='30px'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border='"0'" src='"imgs/cropped"+html['id']+".jpg'" onerror='this.src='"img/no_profile_img.jpeg'"' width='"40'" height='"40'" ></td><td valign='top' align='left'><a href='profile.php?username="+html['username']+"'>"+html['first']+" </a>"+html['comment_content']+"</td></tr></tbody></table></div></div></div></div>");
}
});
return false
});
});
</script>

看起来top函数得到了一个containerId,它用它来知道注释放在哪里。但是,较低的ajax版本总是将注释放入#commentaddid中。目前还不清楚您希望注释出现在DOM中的什么位置,但无论它在哪里,您都需要将#commentaddid选择器替换为它的选择器。