Hi y'all, my name is Elli and I'm new to this forum.
I tried out Ajax for the first time now in my life.
I want to send a value from a selection-list via AJAX to my controller, because I dont want to reload the website.
The problem is, that there is no dump coming, after I typed dd($request). I want to see the data, that is coming in for further editing.
But the Jquery-Script is returning "success".
So I think the request is not reaching the controller. I tried for serveral hours finding a solution but I cant get it. Maybe someone here knows whats going on.
tableselection.blade.php:
<body>
<form id="search-form">
<div id="search-form-filters" data-filters-count="1">
<div class="filter">
<select id="select-options">
<option value="customer">Kunden</option>
<option value="contact_person">Ansprechpartner</option>
</select>
</div>
</div>
<button class="submit-button" type="submit">Suchen</button>
</form>
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#search-form').on('submit', function(event){
event.preventDefault();
table = $('#select-options').val();
$.ajax({
url: '/search-form',
type: "POST",
data:{
table: table,
},
success:function(response){
if (response) {
console.log("success");
$("#search-form")[0].reset();
}
},
error: function() {
console.log("failure");
}
});
});
</script>
</body>
web.php
<?php
Route::get('/',function () {return view('tableselection');});
Route::post('/search-form', [TableSelectionController::class,'gatherData']);
?>
TableSelectionController:
public function gatherData(Request $request)
{
dd(json_encode($request));
}
After trying the devtools out, I got this in preview: "{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}"
I got a 200 and this was the preview. Headers tab contained formdata : table: customer