I'm getting stuck with Ajax, i dont know why my ajax call returns error everytime. My update method dont even detect an ajax request. I've never done ajax before, but i understand the logic behind it so excuse my ignorance if i ask for dumb things lol..
Route::post('edit/organisation/{id}',['uses' => '[email protected]', 'as' => 'admin.organisation.update']);
Here is my controller method :
public function update($route = null, $id, \Illuminate\Http\Request $request)
{
$org = Organisation::find($id)->first();
if($request->ajax())
{
dd('OK ITS AJAX');
}
if($org->update($request->all()))
{
//return redirect('admin/dashboard');
return redirect()->back();
}
}
And here is my ajax
$(document).ready(function()
{
var form = $('.update_ajax');
form.bind('submit',function()
{
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function(data)
{
alert(data);
},
error: function(data)
{
console.log('error');
}
});
});
});
And here is my HTML
{!! Form::model($org,['route' => ['admin.organisation.update',Route::input('name'), $org->id],'class' => 'update_ajax' ]) !!}
{!! Form::label('name','Nom') !!}
{!! Form::text('org_name',null,['class'=> 'form-control'])!!}
{!! Form::label('org_type','Type') !!}
{!! Form::text('org_type',null,['class'=> 'form-control'])!!}
{!! Form::label('siren','Numéro de SIREN') !!}
{!! Form::text('org_siren',null,['class'=> 'form-control']) !!}
{!! Form::label('org_ape_naf','Numéro APE NAF') !!}
{!! Form::text('org_ape_naf',null,['class'=> 'form-control']) !!}
{!! Form::submit('Appliquer',['class' => 'submit-data-btn']) !!}
{!! Form::close() !!}