I'm new to Ajax, but I was able to make a form where I write data -> submit and the controller does it's job. My problem is that I wanted to make a delete button for the records, but it doesn't work, just reloads the page, the route is fine, the alert works fine and the token is fine too.
Here is the code:
The route
Route::post('/delete/{testTaker}', [TestTakerController::class, 'delete']);
The button
<form id="delete">
{{ csrf_field() }}
<button class="deletett" data-id="{{ $testTaker->testTaker }}">delete</button>
</form>
The js part:
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$(".deletett").click(function(){
var testTaker = $(this).data("id");
alert(testTaker);
jQuery.ajax({
url: "/delete/" + testTaker,
method: 'post',
data: {
name: testTaker
},
success: function(result){
jQuery('.alert').show();
jQuery('.alert').html(result.success);
}});
});
</script>