in Ajax : second line -> $(dcument) should not be : $(document) ???
May 3, 2020
5
Level 1
Post with Ajax and Bootstrap Modal
I am using code.jquery.com/jquery-3.5.0.js in my application, and trying to post data without reload. It posts fine without ajax, but not with ajax script. Controllers:
public function index()
{
$tasks = Task::orderBy('id','desc')->paginate(9);
return view('empmodel', compact('tasks'));
}
public function store(Request $request)
{
$tasks = new Task;
$tasks->t_title = $request->input('t_title');
$tasks->project_id = $request->input('project_id');
$tasks->save();
return view('secondp');
}
Ajax:
<script type="text/javascript">
$(dcument).ready(function () {
$('#addform').on('submit', function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "/addstudent",
data: $('#addform').serialize(),
success: function (response) {
console.log(response)
$('#studentaddmodal').modal('hide')
alert("data saved");
},
error: function(error) {
console.log(error)
alert("Data not saved");
}
});
});
});
</script>
</body>
Routes:
Route::get('/tasks', 'projects\ProjectpController@index');
Route::post('/addstudent', 'projects\ProjectpController@store');
CSRF token is also not missing, so what could be the error? It seems not even showing console logs. Update: typo "dcument" changed to "document".
Level 1
Change to
data: {
formData: $('#addform').serialize()
}
and in Controller method
public function store(Request $request)
{
parse_str($request->formData, $output);
$tasks = new Task;
$tasks->t_title = $output['t_title'];
$tasks->project_id = $output['project_id'];
$tasks->save();
}
Please or to participate in this conversation.