Hey there, I’m trying to send notifications from @mzur’s Kirby Uniform using a route and AJAX. Everything worked well from my local dev environment, however as soon as I move it to the server nothing has been getting through.
Any ideas?
'routes' => [
[
'pattern' => 'postman',
'method' => 'POST',
'action' => function () {
$form = new \Uniform\Form([
'email' => [],
'fname' => [],
'lname' => [],
'phone' => [],
'reasons' => [],
'comments' => [],
'subscribe' => []
]);
// Perform validation and execute guards.
$form->withoutFlashing()
->withoutRedirect()->guard();
if (!$form->success()) {
// Return validation errors.
return Response::json($form->errors(), 400);
}
// If validation and guards passed, execute the action.
$form->emailAction([
'to' => 'info@website.com',
'from' => 'info@website.com',
'subject' => 'Web enquiry from {{fname}} {{lname}}',
])->logAction([
'file' => kirby()->roots()->site().'/messages.log'
]);
if (!$form->success()) {
// This should not happen and is our fault.
return Response::json($form->errors(), 500);
}
// Return code 200 on success.
return Response::json([], 200);
// return page('contact');
}
]
JS
// Submit the form using AJAX.
var formData = $(this).serialize();
$.ajax({
type: 'post',
url: $(this).attr('action') + '/postman',
data: formData,
success: function (response) {
// You will get response from your PHP page (what you echo or print)
console.log('success');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
})