Although bootstrapping PW on a file outside PW's directories and calling it with AJAX worked, I am not able to get multi-language field values from it.
So, I am trying to include the contact validation code in a PW template as suggested here: https://processwire.com/talk/topic/4665-accessing-pagetitlelanguage-when-bootstrapped/
The problem is that I can't find how to do that! The following is the code I am using, which works, but is not fetching the various validation messages.
<?php
$emailTo = 'an email';
$name = $input->post['name'];
$email = $input->post['email'];
$message = $input->post['message'];
$form = array(
'name' => $sanitizer->name($name),
'email' => $sanitizer->email($email),
'message' => $sanitizer->textarea($message)
);
$email = "Full name: $form[name]\n" .
"Email: $form[email]\n" .
"Message: $form[message]";
$errors = array();
$data = array();
if (empty($form['name']))
$errors['name'] = $out = __('Name is required');
if (empty($form['email']))
$errors['email'] = $out = __('Email is required.');
if (empty($form['message']))
$errors['message'] = $out = __('Message is required.');
if ( ! empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
} else {
mail($emailTo, "Thassos homes - Επικοινωνία", $email, "From: $form[email]");
$data['success'] = true;
$data['success_message'] = $out = __('Your message has been send! We will reply to you soon.');
}
json_encode($data);
?>
<form action='./' method='post'>
<div>
<input type="text" id="name" name="name" placeholder="name" />
<input type="email" id="email" name="email" placeholder="email" required />
<input type="submit" name="submit" value="send" />
</div>
<textarea id="message" name="message" placeholder="your message" required ></textarea>
<div class="contact_message"></div>
<div class="loading hidden"></div>
</form>
<script>window.jQuery || document.write('<script src="<?php echo $root_dir ?>js/jquery-1.11.0.min.js"><\/script>')</script>
<script>
$(document).ready(function() {
var contact_message = $('.contact_message');
contact_form.submit(function(event) {
contact_message.innerHTML = "";
var contactData = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
'message' : $('textarea[name=message]').val()
};
$.ajax({
type : 'POST',
url : '<?php echo $page->url ?>',
data : contactData,
dataType : 'json'
})
.done(function(data) {
console.log(data);
if ( ! data.success) {
if (data.errors.name) {
contact_message.text(data.errors.name);
}
if (data.errors.email) {
contact_message.text(data.errors.email);
}
if (data.errors.message) {
contact_message.text(data.errors.message);
}
} else {
$('.contact_form div, .contact_form textarea, .loading').addClass('hidden');
$(contact_form).append('<div class="success">' + data.success_message + '</div>');
setTimeout(function(){
contact_form.toggleClass('hidden visible');
}, 3000);
// window.location = 'index.php'; // redirect a user to another page
}
})
.fail(function(data) {
// show any errors
// remove for production
console.log(data);
});
event.preventDefault();
});
})
</script>