Salve a tutti...
Ho inserito un form AJAX all'interno di una pagina del sito su cui sto lavorando.
La piattaforma di sviluppo è Wordpress. Ho deciso per questo script perchè ha degli effetti sul controllo campi e sulla processazione davvero carini.
Ebbene, funziona tutto: l'unico problema è che non viene inviata la mail.
Ho come il sospetto che mi sfugga qualcosa sulla ACTION del form, in relazione a wordpress.
Ecco il codice dello script:
codice:
<script type="text/javascript">
<![CDATA[//><!--
$(document).ready(function() {
$('form#contact-us').submit(function() {
$('form#contact-us .error').remove();
var hasError = false;
$('.requiredField').each(function() {
if($.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).addClass('inputError');
$(this).prev('label').addClass("request");
hasError = true;
}else{
$(this).prev('label').removeClass("request");
}
if($(this).hasClass('email')) {
if($.trim($(this).val()) == '') {
$(this).prev('label').addClass("request");
}else{
$(this).prev('label').removeClass("request");
}
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test($.trim($(this).val()))) {
$(this).prev('label').addClass("request");
var labelText = $(this).prev('label').text();
$(this).addClass('inputError');
hasError = true;
}
}
});
if(!hasError) {
var formInput = $(this).serialize();
$.post($(this).attr('action'),formInput, function(data){
$('.submit').fadeOut('slow', function() {
// Animation complete.
});
$('form#contact-us').delay(800).fadeOut("slow", function() {
if(template_lang == "it-IT"){
$(this).before('<div class="thanks">
Messaggio inviato. Grazie.</p></div>');
}else{
$(this).before('<div class="thanks">
Message sent. Thank you.</p></div>');
}
});
});
}
return false;
});
});
//-->!]]>
</script>
Mi scuso per l'indentatura...
Cosa devo aggiungere per far processare tutto e mandare la mail?
Se dovesse servire, ecco anche il codice PHP per l'invio mail.
(anche quì c'è il controllo degli errori, non so se toglierlo, dato che lo fa jquery)
codice:
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if (isset($_POST['submitted'])) {
// require a name from user
if (trim($_POST['contactName']) === '') {
$nameError = 'Forgot your name!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if (trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if (trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if (function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if (!isset($hasError)) {
$emailTo = 'areaweb@faan.it';
$subject = 'Submitted message from ' . $name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: ' . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
Dimenticavo...
Ecco il form. La sction dovrebbe portare alla pagista stessa!
codice:
<form id="contact-us" action="<?php echo get_template_directory_uri(); ?>" method="post">
Grazie anticipatamente e scusatemi per il poema.
:-)