我正在尝试将我的数据添加到 div 中,但它不起作用。php 很好,但我需要细心看看他们是否能发现任何错误或建议任何变通方法来帮助找出它为什么不起作用。
在过去 5 个小时左右的时间里,我一直试图修复它,这让我绞尽脑汁。
用户评论状态,如帖子,并假设将其添加到 div。就像我的主要状态一样。主要状态的 ajax 几乎与下面的评论 ajax 相同..但是评论 ahax 玩得不好,根本没有插入任何数据。
<script>
$(document).ready(function(){
$("form#mycommentform").submit(function() {
var streamidcontent = $('#streamidcontent').val();
var contents = $(this).children('#contents').val();
$.ajax({
type: "POST",
url: "comment_add.php",
cache: false,
dataType: "json",
data: { streamidcontent: streamidcontent, contents: contents},
success: function(data){
$('#containerid').html('<div class="stream_comment_holder" style="display:none;"
id="comment_holder_'+data['comment_streamitem']+'"><div
id="comment_list_'+data['comment_streamitem']+'"><div
id="tgy"></div><div class="stream_comment"
id="comment_'+data['comment_id']+'" style="margin-top:0px;"><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'+data['id']+'.jpg\"
onerror="this.src=\"img/no_profile_img.jpeg\"" width=\"40\"
height=\"40\" ></a><td valign=top align=left><a
href="/profile.php?username='+data['username']+'">'+data['first']+'</a><div
class="commentholder">'+data['first']+'</div><br/><div
id="commentactivitycontainer"></div></div></table></div></div><div
class="form"><form id="mycommentform" method="POST"
class="form_statusinput"><input type="hidden" name="streamidcontent"
id="streamidcontent" value="'+data['comment_streamitem']+'"><input
type="text" name"contents" id="contents" placeholder="Say something"
autocomplete="off"><input type="submit" id="button"
value="Feed"></form></div><div class="stream_comment_holder"
style="display:;"><div class="like_name"><b><a
href="profile.php?username='+data['username']+'">You Like
This</a></b></div></div>');
}
});
return false
});
});
</script>
发现错误
success: function (data) {
alert("SUCCESS!!!");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.status);
alert(thrownError);
}
OK
200
SyntaxError.JSONparse:Unexpectedcharacter
JSON
$json = array();
$check = "SELECT comment_id, comment_datetime, comment_streamitem FROM streamdata_comments WHERE comment_streamitem=".$_POST['streamidcontent']."";
$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);