I can't figure it out why I am getting error "JSON.parse: unexpected character" in Firefox console.. Looks like the problem is around jQuery.parseJSON(data)); in comment.js file.. If I remove both if statements in my php file works everything well, but this is not what I need..
Thanks!
comment.php
<?php
require_once ("../modules/comments.php");
require_once ("../includes/db_connect.php");
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert' ) {
$userId = (int)$_POST['userId'];
//$comment = addslashes(str_replace("\n", "<br>", $_POST['comment']));
$comment = strip_tags($_POST['comment']); // remove html tags
$comment = ucfirst(strtolower($comment));
//$comment = addslashes(str_replace("\n", "<br>"));
$std = new stdClass();
$std->commentId = 24;
$std->userId = $userId;
$std->comment = $comment;
$std->userName = "John Stu";
$std->profile_img = "https://s.ytimg.com/yts/img/avatar_48-vfllY0UTT.png";
if(class_exists('Comments') && class_exists('Subscribers')) {
$commentInfo = Comments::insertComments($comment, $userId);
if($commentInfo != null) {
}
}
echo json_encode($std);
} else {
//header("Location: /");
}
?>
comment.js
$(document).ready(function(){
$('#post-comment-btn').click(function(){
comment_post_btn_click();
});
});
function comment_post_btn_click(){
var _comment = $('#comment-post-text').val();
var _userId = $('#userId').val();
var _userName = $('#userName').val();
if(_comment.length > 0 && _userId != null) {
$.post("ajax/comment_insert.php", {
task : "comment_insert",
userId : _userId,
comment : _comment,
}).done(function(data) {
comment_insert(jQuery.parseJSON(data));
console.log("ResponseText:" + data);
});
console.log(_comment + " Username: " + _userName + " User Id: " + _userId);
} else {
console.log("The text area is empty..");
}
$('#comment-post-text').val("");
}
function comment_insert(data) {
var t = '';
t += '<li class="comment-holder" id="_'+data.comment_id+'">';
t += '<div class="user-img">';
t += '<img src="'+data.profile_img+'">';
t += '</div>';
t += '<div class="comment-body">';
t += '<h3 class="username-field">'+data.userName+'</h3>';
t += '<div class="comment-text">'+data.commen+'</div>';
t += '</div>';
t += '<div class="comment-buttons-holder">';
t += '<ul>';
t += '<li class="delete-btn">X</li>';
t += '</ul>';
t += '</div>';
t += '</li>';
$('.comments-holder-ul').prepend(t);
}
phpcode.. usetry..catchandreturn exceptionas json response.. then you'll be able to see what is php error.