Website URL
https://www.gr8brik.rf.gd/creation.php?id=10#c43
Error Message
AJAX error: 200 parsererror SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
jQuery 3
s
l
o
Other Information
This happens when I try to delete a comment on my website.
The proccess is simple (but complex). The Rendered HTML has a delete button for admins, and Jquery to handle things like JSON and posting+errors. Finally, the delete comment PHP connects to the database and deletes the comment. One small problem: I havenât been able to get a jQuery method to work. I am new to jQuery and post/network requests with it.
Jquery: >
$(document).ready(function() { $('.delete-comment').click(function() { var commentId = $(this).data('comment_id_id'); if (confirm('Are you sure you want to delete this comment? If you delete a comment it cannot be recovered.')) { $.ajax({ url: 'creation.php', type: 'POST', dataType: 'json', data: { comment_id: commentId }, success: function(response) { console.log(response) if (response === undefined || response === '') { alert('Empty response received.'); console.log('Response:', response); } else { if (response.success) { $(this).closest('#post').remove(); // Optionally, add a success message or update the UI further } else { alert('Error: ' + response.error); console.log('commentId:', commentId); } } }, error: function(xhr, status, error) { alert('Error deleting comment: ' + error); console.log('AJAX error:', xhr.status, status, error); } }); } }); }); </script>
HTML/Render PHP: > <?php
// Create connection $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME2); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $model_id = $_GET['id']; $sql = "SELECT id, user, model, comment, date FROM comments WHERE model = ? ORDER BY date ASC"; $stmt = $conn->prepare($sql); if (!$stmt) { die("Prepare failed: " . $conn->error); } $stmt->bind_param("i", $model_id); $stmt->execute(); $comResult = $stmt->get_result(); if ($comResult->num_rows > 0) { while ($row = $comResult->fetch_assoc()) { $c_id = $row['id']; $c_user = $row['user']; $c_comment = $row['comment']; $c_model = $row['model']; $c_date = $row['date']; // Create connection $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT username FROM users WHERE id = $c_user"; $userResult = $conn->query($sql); $userRow = $userResult->fetch_assoc(); $c_username = $userRow['username']; echo '<div id="post"><img id="pfp-post" src="acc/users/pfps/' . $c_user . '..jpg">'; echo '<article class="w3-card-24 w3-light-grey w3-padding-small" style="width:100%">'; echo '<header><b><a href="user/' . strtolower($c_username) . '">' . $c_username . '</a>'; echo '<a id="c' . $c_id . '" href="#c' . $c_id . '"><time style="float:right;" datetime="' . $c_date . '">' . $c_date . '</time></a></b>'; if ($admin === 1 || trim($_SESSION['username']) === trim($c_user)) { echo '<button class="delete-comment w3-btn w3-blue w3-hover-opacity w3-right" data-comment_id_id="' . $c_id . '">DELETE</button>'; //echo '<form method="post" action=""><input type="text" name="comment_id_id" id="comment_id_id" value="' . $c_id . '" style="display:none" /><p><input class="w3-btn w3-blue w3-hover-blue" type="submit" id="comment_id" value="DELETE" name="comment_id" /></p></form>'; } echo '</header>'; echo '<h4>' . $bbcode->toHTML($c_comment) . '</h4>'; echo '</article></div><br />'; } } else { echo "<b>Nothing here yet</b><br />"; echo "<img src='img/empty.png' style='width:auto;height:auto;border:5px solid;border-radius:5px;'>"; } echo '</div><br />'; include('linkbar.php'); ?>
Delete comment PHP:
if (isset($_POST[âcomment_idâ])) {
if (isset($_POST[âcomment_idâ])) {$commentId = $_POST['comment_id_id']; define('DB_NAME2', 'if0_36019408_creations'); $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME2); if ($conn->connect_error) { $response = ['success' => false, 'error' => 'Database connection error']; header('Content-Type: application/json'); echo json_encode($response); die(); } $sql = "DELETE FROM comments WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $commentId); if ($stmt->execute()) { //echo "200 OKAY"; $response = ['success' => true]; } else { $response = ['success' => false, 'error' => $stmt->error]; } $stmt->close(); $conn->close(); header('Content-Type: application/json'); echo json_encode($response); } else { $response = ['success' => false, 'error' => 'Missing comment ID']; header('Content-Type: application/json'); echo json_encode($response); die(); }
}