my code works alright but the alert message in the browser doesn’t go away after I click the okay button inside the alert success prompt. I’ve tried all I can but still persists. Please help.
HTML
<button id="flag-post" data-post-id="{{ $counter->id }}">Report this</button>
Route
Route::get('/flag', [AppHttpControllersApplicationsController::class, 'flagPost'])->name('flag.post');
Controller
public function flagPost(Request $request)
{
$postId = $request->input('post_id');
$post = Job::findOrFail($postId);
$post->increment('flags');
return response()->json(['success' => true]);
}
Javascript
<script>
$(function() {
$('#flag-post').click(function() {
// Retrieve the post ID from the button's data attribute
var postId = $(this).data('post-id');
// Send an AJAX request to the flag post route
$.ajax({
url: "{{ route('flag.post') }}",
type: "GET",
data: {
post_id: postId,
_token: "{{ csrf_token() }}"
},
success: function(response) {
// Alert the user that the post has been flagged
alert("Thank you for flagging this post!");
},
error: function(xhr, status, error) {
// Alert the user that there was an error
alert("Error: " + error);
}
});
});
});
</script>