6
I have a contact form where I send the information entered to my BD, I would like to leave a message to the user after submitting the form and I am not succeeding, what I did was this:
I have a div positioned above my form, thus:
<div class="loader"></div>
By submitting the form I do a validation of the fields of the same and having send the message, it is like this:
$(document).ready(function(){
// Scripts de submissão
$('.contactForm').submit( function(){
//statements to validate the form
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var email = document.getElementById('e-mail');
if (document.cform.nome.value == "") {
$('.nome-missing').css({'opacity': 1 });
} else {$('.nome-missing').css({'opacity': 0 });}
if (document.cform.telefone.value == "") {
$('.telefone-missing').css({'opacity': 1 });
} else {$('.telefone-missing').css({'opacity': 0 });}
if (!filter.test(email.value)) {
$('.email-missing').css({'opacity': 1 });
} else {$('.email-missing').css({'opacity': 0 });}
if (document.cform.cidade.value == "") {
$('.cidade-missing').css({'opacity': 1 });
} else {$('.cidade-missing').css({'opacity': 0 });}
if (document.cform.uf.value == "") {
$('.uf-missing').css({'opacity': 1 });
} else {$('.uf-missing').css({'opacity': 0 });}
if (document.cform.assunto.value == "") {
$('.assunto-missing').css({'opacity': 1 });
} else {$('.assunto-missing').css({'opacity': 0 });}
if (document.cform.mensagem.value == "") {
$('.mensagem-missing').css({'opacity': 1 });
} else {$('.mensagem-missing').css({'opacity': 0 });}
if ((document.cform.nome.value == "") ||
(document.cform.telefone.value == "") ||
(!filter.test(email.value)) ||
(document.cform.cidade.value == "") ||
(document.cform.uf.value == "") ||
(document.cform.assunto.value == "") ||
(document.cform.mensagem.value == "")) {
return false;
}
if ((document.cform.nome.value != "") &&
(document.cform.telefone.value != "") &&
(!filter.test(email.value)) &&
(document.cform.cidade.value != "") &&
(document.cform.uf.value != "") &&
(document.cform.assunto.value != "") &&
(document.cform.mensagem.value != "")) {
// Mostrar a barra de carregamento
$('.loader').append();
//send the ajax request
$.post('processo.php',{
nome:$('#nome').val(),
telefone:$('#telefone').val(),
email:$('#e-mail').val(),
cidade:$('#cidade').val(),
uf:$('#uf').val(),
assunto:$('#assunto').val(),
mensagem:$('#mensagem').val()
},
// Retornar os dados
function(data){
// Oculta
$('.loader').append(data).slideDown(800);
});
// Espera 2000, em seguida, fecha o formulário e desaparece
setTimeout('$(".resposta").slideUp(800)', 6000);
// Permanecer na página
return false;
}
});
});
My .php is like this:
$nome = $_POST['nome'];
$email = $_POST['email'];
$telefone = $_POST['telefone'];
$cidade = $_POST['cidade'];
$assunto = $_POST['assunto'];
$mensagem = nl2br($_POST['mensagem']);
$subject = $assunto;
$body = "From $nome, \n\n$mensagem";
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail("[email protected]", $subject, $body, $headers);
<div class="resposta">
<h4>Obrigado <?php echo $nome ?>!</h4>
<p>Em breve entraremos em contato com você.</p>
The css of div answer is like this:
.resposta {
border-bottom: 1px solid #eee;
padding-bottom: 15px;
margin-bottom: 15px;
}
I believe there are better and simpler ways to make this submission using the method
$.ajax({ ... }). As for validation, it is easier and safer to do this directly in PHP and return the message via ajax. I’ll try to come up with an answer, only it’ll be long.– Ivan Ferrer