I have this backend php file and in this file there is this code :
if (isset($_POST['send_comment'])) {
$post_id = $_POST['post_id'];
$community_id = $_POST['community_id'];
$comment_content = $_POST['comment_content'];
date_default_timezone_set('UTC');
$date = date('Y-m-d H:i:s T', time());
$sql = "INSERT INTO comments (user_id, community_id, post_id, comment, date_time) VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "There was a problem, please try again";
} else {
mysqli_stmt_bind_param($stmt, "iiiss", $_SESSION['user-id'], $community_id, $post_id, $comment_content, $date);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
}
echo $result;
}
I have a .js function that console.log(this.responsetext). But in the console it displays nothing. There is a line, but it echoed "". There are no errors, and the sql statement doesn't work, nothing is added to the database. I don't know if this will help but here is the js code that calls this :
function sendcomment(post_id, community_id) {
comment_content = document.getElementById(post_id).getElementsByClassName('comment-send')[0];
if (comment_content.value == "") {
alert("You have to to have something in your comment to send it!");
} else if (comment_content.textLength > 2000) {
alert("Sorry but your comment is too long!");
} else {
var fd = new FormData();
fd.append("post_id", post_id);
fd.append("community_id", community_id);
fd.append("comment_content", comment_content.value);
fd.append("send_comment", true)
var xhr = new XMLHttpRequest();
var fullurl = '../backend/communitybackend.php';
xhr.open('POST', fullurl, true);
xhr.onload = function() {
if (this.status == 200) {
console.log(this.responseText);
commentsAppear(post_id, community_id);
}
}
xhr.send(fd);
}
}
Thanks!