3
I have this contact form and I would like to be shown all validation errors when you have, for example, if you leave the field name and email without filling, when you gave Ubmit return these two errors, in the current code only shows an error, even if you have others, I’ve searched several codes on the internet and I couldn’t solve it, someone can help me?
css style.
*,
*: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;
}
?>
Opa @Miguel solved yes man, I do not know what happened, I marked again, thank you very much, now I put the ajax and got mass, but to see some people below say that it is not recommended to use so many ifs
– Sérgio Machado
Don’t worry, it doesn’t matter and it doesn’t hurt to use the if you need it.... The only thing about using 9999 ifs is that the code gets very extensive... Otherwise, if they are well structured, there is nothing else. However feel free to choose any one solutions below
– Miguel
I understood perfectly @Miguel, a very simple thing that solved my problem, I think this collaboration of people in helping each other. in that same form I added the image field and whenever I give Submit returns a PHP error I do not if it is the ajax that is interfering.
– Sérgio Machado
Without seeing I do not know. The best thing to do is to ask another question with relevant code and explain the problem. I and other colleagues try to help you.
– Miguel