Do you have form closing tag?
Aug 7, 2017
7
Level 1
Laravel use ajax on item list
Hello,
i want to update a boolean attribute with one click with an ajax request I have students that can be absents
here is my view
| User | Justified |
| ------ | ---------- |
| Sam | Yes |
| John | No |
| Doe | Yes |
| Jack | No |
| ...... | ......... |
My problem is that when il click on it to update the "justified" attribute, it change the page and show me a blank page with the result of justifiedToggle function ("Success for xx").
here is my form foreach users
<form action="'.route('absence.justifiedtoggle').'" method="POST" class="formJustifiedToggle">
<input name="_method" type="hidden" value="POST">
<input name="absence_id" type="hidden" value="'.$absence->id.'">
<input name="_token" type="hidden" value="'.csrf_token().'">
my route
Route::post('absence/justifiedtoggle', 'AbsenceController@justifiedToggle')->name('absence.justifiedtoggle');
and my controller
public function justifiedToggle(Request $request){
$absence = Absence::findOrFail($request->get('absence_id'));
$absence->toggleJustified()->save(); // if false put true, if true put false
return 'Success for '. $absence->id;
}
Here my js
$('.formJustifiedToggle').submit(function(e){
e.preventDefault();
$.ajax({
method: 'POST',
url: APP_URL + '/absence/justifiedtoggle',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}).done(function( msg ) {
console.log( "Response: " + msg );
});
});
thanks for your time som
Level 1
Please or to participate in this conversation.