Reputation: 749
I'm new to Laravel and trying to send emails via contact form.
This is the html form:
<form onsubmit="return false" method="post" id="contact_form" name="contact-form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" placeholder="Full Name" name="fullname" id="fullname" required>
<input placeholder="Email Address" required name="email" id="email">
<button id="contact_btn"data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Sending" >Send Your Message</button>
</form>
This is the controller:
public function contactPost(Request $request) {
$this->validate($request, [
'email'=>'required|email',
'fullname'=>'max:50',
]);
$data = array(
'email' => $request->email,
'fullname' => $request->fullname,
);
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('**@gmail.com');
});
return ['success' => true,];
}
This is the route:
Route::post('contact', 'PagesController@contactPost');
This is the last error I could find on laravel.logs:
[2017-08-11 06:30:53] local.ERROR: ErrorException: Use of undefined constant csrf_token - assumed 'csrf_token' in C:\xampp\htdocs\paygizer\storage\framework\views\b9aff2e30c0efeff3b03704fbc6a957f360869cf.php:385
No matter what I do the error doesn't update since yesterday, I restarted the server several times.
This is the ajax call:
$('#contact_form').submit(function(e) {
e.preventDefault();
}).validate({
rules: {
fullname: {
required: true,
},
email: {
required: true,
email: true
},
},
submitHandler: function (form) {
var btn = $('#contact_btn');
btn.button('loading');
setTimeout(function() {
btn.button('reset');
}, 8000);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
url: "{{url('contact')}}",
// data: form.serialize(),
dataType: 'json',
async: true,
data: {
fullname: $('#fullname').val(),
email: $('#email').val(),
},
success: function (json) {
$('#contact').hide();
$('#output').html(
'<i class="fa fa-check" aria-hidden="true" id="check" style="border-radius:50%;font-size:80px;text-align:center;color:#E81B00"></i><br><p class="lead" style="font-size:40px;">We have received your message!</p>'
).show();
},
error: function(data){
alert(data);
},
});
return false; // for demo
}
});
No errors on Chrome dev tools. When I hit submit the button loads and nothing happens.
Upvotes: 0
Views: 53
Reputation: 2468
1) try replacing
<input type="hidden" name="_token" value="{{ csrf_token() }}">
with
{{ csrf_field() }}
2) since it is an ajax call are you sure csrf-token meta exists?
<meta name="csrf-token" content="{{ csrf_token() }}">
Upvotes: 1