I am trying to submit a form using ajax
so my page will not do a full page reload
rather only the form should change to show any errors. I have tried many options from the web with no luck. This here is the best help I have seen, but
it doesn't give the solution. Note I am using laravel version 5.3.10
.
Here is my code:
Javascript:
<script type="text/javascript">
$(document).ready(function () {
$('#footer-form').click(function () {
//event.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/ensignhospital/mail',
data: $('form#footer-form').serialize(),
dataType: 'html'
})
.done(function (data) {
console.log(data);
});
});
});
</script>
Laravel Route:
Route::group(['before' => 'guest'], function () {
/*
* CSRF Protection
*
* */
Route::group(['before' => 'csrf'], function () {
Route::post('/ensignhospital/mail', [
'as' => 'mail',
'uses' => 'HomeController@postSendMail'
]);
});
});
Laravel controller:
public function postSendMail(Request $request)
{
if($request->ajax()){
$validator = Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'message' => 'required',
'g-recaptcha-response' => 'required|captcha'
]);
if($validator->fails()){
return redirect()->back()
->withErrors($validator)
->withInput(Input::all());
}else{
return View('passed');
}
}else{
return View('fail');
}
}
Form:
{!! Form::open(['route' => 'mail', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!}
<div class="form-group has-feedback">
{!! Form::label('first_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
@if($errors->has('first_name'))
{{ $errors->first('first_name') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('last_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
@if($errors->has('last_name'))
{{ $errors->first('last_name') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('email', null, ['class' => 'sr-only']) !!}
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!}
<i class="fa fa-envelope form-control-feedback"></i>
@if($errors->has('email'))
{{ $errors->first('email') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('message', null, ['class' => 'sr-only']) !!}
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => 8, 'placeholder' => 'Message']) !!}
<i class="fa fa-pencil form-control-feedback"></i>
@if($errors->has('message'))
{{ $errors->first('message') }}
@endif
</div>
{!! Form::submit('Send', ['class' => 'btn btn-default', 'id' => 'mail_btn']) !!}
{!! Form::close() !!}
What you need to do is to render the view that you want to pass throught the ajax like this:
return view('passed')->render();
...
return view('fail')->render();
EDIT
You have to pass event
as a clouser parameter and uncomment this: //event.preventDefault();
:
$('#footer-form').click(function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/ensignhospital/mail',
data: $('form#footer-form').serialize(),
dataType: 'html'
})
.done(function (data) {
console.log(data);
});
});
so that:
click(function () {
why it doesn't work. Should be:
click(function (event) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.