Leon Burning Leon Burning - 3 months ago 6
HTML Question

Why isn't my comment getting posted?

Html part:



<div class="page-data">
Page data is in here.
</div>

<div class="comment-wrapper">
<h3 class="comment-title">Feedback:</h3>

<div class="comment-insert">

<h3 class="who-says">Says: Leon Burning</h3>
<div class="comment-insert-container">
<textarea id="comment-post-text" class="comment-insert-text"></textarea>
</div>

<div class="comment-post-btn-wrapper" id="comment-post-btn">
Post
</div>
</div>

<div class="comments-list">

<ul class="comments-holder-ul">
<?php $comments = array( "a", "b", "c" , "d"); ?>
<?php require_once INCLUDES . 'comment_box.php';?>
</ul>
</div>
</div>

</div>

<input type="hidden" id="userId" value="1" />
<input type="hidden" id="userName" value="Leon Burning" />

</body>


<---JavaScript (comment_insert.js)---->

$(document).ready( function(){
$('#comment-post-btn').click(function() {
comments_post_btn_click();
});
});
function comments_post_btn_click() {
var _comment = $('#comment-post-text').val();
var _userId = $( '#userId' ).val();
var _userName = $( '#userName' ).val();

if( _comment.length > 0 && _userId !=null ) {
$('#comment-post-text').css('border','0px solid red');

$.ajax({
type: "POST",
url: "ajax/comments_insert.php",
data: {
task : "comment_insert",
userId : "userid",
comment : _comment
},
success : function(data)
{
comment_insert( jQuery.parseJSON(data));
console.log("ResponseText " + data); },
});

console.log(_comment + " / Username: " + _userName + " / User-ID " + _userId );
}
else {
$('#comment-post-text').css('border','1px solid red');
console.log("Area was empty.");
}

$('#comment-post-text').val("");

}

function comment_insert() {
var t = "";
t += '<li class="comments-holder" id"_1">';
t += '<div class="user-img">';
t += '<img src="user_img.png" class="user-img-pic" />';
t += '</div>';
t += '<div class="comment-body">';
t += '<h3 class="username-field">Insert User</h3>';
t += '<div class="comment-text">New Comment</div>';
t += '<div class="comment-buttons-holder">';
t += '<ul>';
t += '<li class="delete-btn">X</li>';
t += ' </ul>';
t += '</div>';
t += '</div>';
t += '</li>';

$( '.comments-holder-ul' ).prepend( t );
}


<---comments_insert.php--->

<?php

print_r( $_REQUEST );

?>


Firefox Error Message:

"SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data"

I would be very grateful if anyone could fix this for me! :)

Answer

You are trying to parse the response from your ajax call as JSON:

success : function(data) { comment_insert( jQuery.parseJSON(data)); console.log("ResponseText " + data); }, });

This will not work since you are just printing out the $_REQUEST array:

print_r( $_REQUEST );

You will need to convert the array to JSON first:

echo json_encode( $_REQUEST );
Comments