1
Verification of a contact form with success messages and errors
Attention the corrections I’ve made over time!
PHP CODE
if (isset($_POST['contact'])) {
$nomecomp = test_input($_POST["nomecomp"]);
if (!preg_match("/^[a-zA-Z ]*$/",$nomecomp)) {
$nomecompErr = "Apenas letras e espaços permitidos";
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Email inválido";
}
$assunto = test_input($_POST["assunto"]);
if (!preg_match("/^[a-zA-Z ]*$/",$assunto)) {
$assuntoErr = "Apenas letras e espaços permitidos com máximo de 25
carácteres";
}
$mensagem = test_input($_POST["mensagem"]);
if (!preg_match("/^[a-zA-Z ]*$/",$mensagem)) {
$mensagemErr = "Apenas letras e espaços permitidos";
}
else
{
echo '<script>',
'jsFunction();',
'</script>';
$mailto = "[email protected]";
$headers = "From:". $email;
$txt = "Recebes-tes um email de ". $nomecomp . ".\n\n".$mensagem;
mail($mailto, $assunto,$txt,$headers);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
FORM
<form method="post" role="form" class="contactForm" action="">
<div class="col-md-6 col-sm-6 col-xs-12 left">
<div class="form-group">
<span class="errormessage">* <?php echo $nomecompErr;?> *</span>
<input type="text" name="nomecomp" class="form-control form"
id="name" placeholder="Nome Completo" REQUIRED />
</div>
<div class="form-group">
<span class="red">* <?php echo $emailErr;?>*</span>
<input type="email" class="form-control" name="email" id="email"
placeholder="Email"REQUIRED />
</div>
<div class="form-group">
<span class="error">* <?php echo $assuntoErr;?>*
</span>
<input type="text" class="form-control" name="assunto"
id="subject" placeholder="Assunto" maxlength="25" REQUIRED/>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 right">
<div class="form-group">
<span class="error">* <?php echo $mensagemErr;?>*</span>
<textarea class="form-control" name="mensagem" rows="5"
placeholder="Mensagem" REQUIRED></textarea>
</div>
</div>
<div class="col-xs-12">
<!-- Button -->
<button type="submit" id="submit" name="contact" class="form
contact-form-button light-form-button oswald light fundoazul">Enviar
email</button>
</div>
</form>
JS
function jsFunction() {
$.bootstrapGrowl("A sua mensagem foi enviada com sucesso!", { type: 'success' });
}, 1000;
</script>
Library
<script src="js/jquery.bootstrap-growl.min.js" type="text/javascript">
</script>
Resolution with PHP and JS
Novo Javascript
$( document ).ready(function() {
var aux= "<?php echo($_SESSION['varphp']); ?>";
if(aux == 1) {
$.bootstrapGrowl("A sua mensagem foi enviada com sucesso", {
type: 'success' });
}
});
</script>
PHP
Declare the session variable
$_SESSION["varphp"]=0;
And change this:
echo '<script>',
'jsFunction();',
'</script>';
For this reason:
$_SESSION["varphp"]=1;
email is being sent correctly?
– Wees Smith
Yes, I’m already in my domain!
– AlmostDone
I’m trying to solve my problem differently now!
– AlmostDone
Edited answer! and with the explanation of how to send the user, after submission, to the part of the page where the form is
– user60252