I have this contact form and would like you to see all validation errors when you have, for example, if you leave the name and email field unfilled, when submitting the return returned these two errors, in the current code only shows an error, even if I have others, I already looked for several codes on the internet and I could not solve this, can anyone help me?
style.css
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-size: 14px;
background: #F7F7F7;
color: #333;
}
.clear {
clear: both;
}
.container {
max-width: 700px;
margin: 0 auto;
}
h1 {
color: #333;
text-align: center;
}
p {
font-size: 16px;
text-align: center;
}
.formulario {
border: solid 1px #ccc;
padding: 20px;
border-radius: 5px;
width: 100%;
float: left;
}
input:focus::-webkit-input-placeholder {
color: transparent;
}
input:focus:-moz-placeholder {
color: transparent;
}
/* FF 4-18 */
input:focus::-moz-placeholder {
color: transparent;
}
/* FF 19+ */
input:focus:-ms-input-placeholder {
color: transparent;
}
/* IE 10+ */
textarea:focus::-webkit-input-placeholder {
color: transparent;
}
textarea:focus:-moz-placeholder {
color: transparent;
}
/* FF 4-18 */
textarea:focus::-moz-placeholder {
color: transparent;
}
/* FF 19+ */
textarea:focus:-ms-input-placeholder {
color: transparent;
}
/* IE 10+ */
.nome,
.email,
.telefone,
.assunto {
height: 50px;
width: 100%;
float: left;
padding: 0px 10px;
margin-bottom: 20px;
font-size: 20px;
border: solid 1px #ccc;
border-radius: 3px;
color: #777;
}
.email,
.assunto {
float: right;
}
.formulario .mensagem {
border: solid 1px #ccc;
width: 100%;
height: 150px;
float: left;
font-size: 16px;
color: #777;
margin-bottom: 20px;
border-radius: 3px;
padding: 10px;
}
.formulario .enviar {
background: #58D68D;
color: #fff;
text-transform: uppercase;
border: none;
cursor: pointer;
text-align: center;
padding: 15px 0px;
width: 100%;
font-size: 18px;
border-radius: 3px;
}
.formulario .enviar:hover {
background: #2ECC71;
}
index.html
<div class="container">
<form class="formulario" action="enviar.php" method="POST">
<input class="nome" type="text" name="nome" placeholder="Nome" />
<input class="email" type="text" name="email" placeholder="E-Mail" />
<input class="telefone" type="text" name="telefone" placeholder="Telefone" />
<input class="assunto" type="text" name="assunto" placeholder="Assunto" />
<textarea class="mensagem" name="mensagem" placeholder="Digite sua mensagem"></textarea>
<input class="enviar" name="enviar_email" type="submit" value="Enviar Dados" />
</form>
<div class="clear"></div>
</div>
send.php
require_once 'phpmailer/class.phpmailer.php';
require_once 'phpmailer/class.smtp.php';
if (isset($_POST['enviar_email'])):
$nome = strip_tags(trim($_POST['nome']));
$email = strip_tags(trim($_POST['email']));
$telefone = strip_tags(trim($_POST['telefone']));
$assunto = strip_tags(trim($_POST['assunto']));
$mensagem = strip_tags(trim($_POST['mensagem']));
$erro = array();
if (empty($nome)):
$erro[] = "Digite seu nome";
elseif (empty($email)):
$erro[] = "Digite um email";
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)):
$erro[] = "E-Mail invalido";
elseif (empty($telefone)):
$erro[] = "Digite seu telefone";
elseif (empty($assunto)):
$erro[] = "Digite um assunto";
elseif (empty($mensagem)):
$erro[] = "Digite uma mensagem";
else:
$Email = new PHPMailer();
$Email->setLanguage('br');
$host = '';
$username = '';
$senha = '';
$porta = '';
$secure = '';
$receber_email = '[email protected]';
$receber_nome = 'Sérgio Machado';
$from = $username;
$fromName = 'Sérgio';
$Email->isSMTP();
$Email->Host = $host;
$Email->SMTPAuth = true;
$Email->Username = $username;
$Email->Password = $senha;
$Email->Port = $porta;
$Email->SMTPSecure = $secure;
$Email->From = $from;
$Email->FromName = $fromName;
$Email->addReplyTo($email, $nome);
$Email->addAddress($receber_email, $receber_nome);
$Email->isHTML(true);
$Email->CharSet = 'utf-8';
$Email->WordWrap = 70;
$Email->Subject = $assunto; //Assunto
$Email->Body = $mensagem;
$enviado = $Email->send();
if ($enviado) {
echo 'E-mail enviado com sucesso';
} else {
echo 'Error: ' . $Email->ErrorInfo;
}
endif;
endif;
foreach ($erro as $err) {
if (!empty($err)):
echo $err;
endif;
}
?>