Hi,
I have a contact form on the homepage and it works fine, but I want to make to submit the form via AJAX.
So I added a script that does that, I have that same script running on a different site here on my local system and it is working fine.
But if I add it to this site I can’t get it to work, can anybody gives me some pointers why it is not working?
Homepage controller
private static $allowed_actions = array(
'ContactForm',
'handleContactForm'
);
public function ContactForm(){
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
LiteralField::create('wrapper', '<div class="col-md-6">'),
TextField::create('Name','Name')->setAttribute('placeholder', 'Name'),
EmailField::create('Email','Email')->setAttribute('placeholder', 'Email'),
TextField::create('Phone','Phone')->setAttribute('placeholder', 'Phone'),
LiteralField::create('wrapper', '</div>'),
LiteralField::create('wrapper', '<div class="col-md-6">'),
TextareaField::create('Message','Message')->setAttribute('placeholder', 'Message'),
LiteralField::create('wrapper', '</div>')
),
FieldList::create(
LiteralField::create('wrapper', '<div class="col-md-12 text-center">'),
FormAction::create('handleContactForm','Submit')->addExtraClass('submit')
->setUseButtonTag(false),
LiteralField::create('wrapper', '</div>')
),
RequiredFields::create('Name','Email','Message')
)
->addExtraClass('form general-validate');
Requirements::javascript('mysite/site/page/contact.js');
// load form state
$data = Session::get("FormData.{$form->getName()}.data");
$form->setTemplate('ContactForm');
return $data ? $form->loadDataFrom($data) : $form;
}
public function handleContactForm($data, $form) {
// set form state
Session::set("FormData.{$form->getName()}.data", $data);
$email = new Email();
$email->setTo('my@domain.com');
$email->setFrom($data['Email']);
$email->setSubject("[Website feedback] Message from {$data["Name"]}");
$messageBody = "
<p><strong>Name:</strong> {$data['Name']}</p>
<p><strong>Phone:</strong> {$data['Phone']}</p>
<p><strong>Message:</strong> {$data['Message']}</p>
";
$email->setBody($messageBody);
$success = $email->send();
// clear form state if success
Session::clear("FormData.{$form->getName()}.data");
if ($success) {
$output['status'] = 'success';
$output['message'] = 'You message has been send';
}else{
$output['status'] = 'danger';
$output['message'] = 'You message has NOT been send';
}
if(Director::is_ajax()) {
return json_encode($output);
}else{
$form->sessionMessage('Thanks for your submission, your message has been send','good');
return $this->redirectBack();
}
}
contact.js
$(function() {
// Get the form.
var form = $('#Form_ContactForm');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
var url = $(form).attr('action');
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: url,
data: formData,
dataType: 'json'
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
if(response.status == 'danger'){
$(formMessages).addClass('alert alert-danger');
}else{
$(formMessages).removeClass('alert alert-danger');
$(formMessages).addClass('alert alert-success');
// Clear the form.
$('#Form_ContactForm_Name').val('');
$('#Form_ContactForm_Email').val('');
$('#Form_ContactForm_Phone').val('');
$('#Form_ContactForm_Message').val('');
}
$(formMessages).html(response.message);
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('alert alert-success');
$(formMessages).addClass('alert alert-danger');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occurred and your message could not be sent.');
}
});
});
});
Thanks