This is so weird I had to record a gif to go along with my explanation so that I could present it clearer.
Now here's my explanation on the steps I take in this gif.
- I create 3 comments called "First", "Second" and "Third".
- I refresh the phpMyAdmin so you can see that they're in fact created.
- I inspect their hidden input values so we can confirm that they hold the correct and respective IDs.
- Then I delete the "Third" comment and refresh the database so you could see that the correct comment has been removed.
- This is where the problem begins. I click to delete "First" comment and on my page it looks like I have actually deleted the "First" comment, however, in the database we can see that in reality, "Second" comment has been deleted and that first is still left there.
- Finally, when I try to delete "Second" comment on the page, I get a server error in the console. The error is that I'm trying to delete a non-existent record.
- Meanwhile, comment "First" still exist in the database.
My form for deletion:
<form method='POST' action=''>
<input type="hidden" name="comment_id" value="">
<button class='submit-btn delete-comment' type='submit' name='commentDelete'>X</button>
</form>
JavaScript Ajax:
$('.delete-comment').on('click', function(event) {
event.preventDefault();
var button = $(this);
var flexbox = button.parents().eq(2);
var commentId = $("input[name=comment_id]").val();
$.ajax({
method: 'POST',
url: urlDeleteComment,
data: {
commentId: commentId,
_method: 'delete',
_token: token
}
}).done(function(response) {
flexbox.remove();
})
});
Comment deletion function:
public function deleteComment(Request $request){
$commentId = $request['commentId'];
$comment = Comment::find($commentId);
$comment->delete();
}
Any help would be appreciated since I thought about the issue for quite some time and I haven't figured where my problem is.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire