1
When I submit the form it is checking if all fields have been filled. I want only name, email and phone to be mandatory for the form to be submitted.
I have the following code
<?php
$subjectPrefix = '[Matricula - Master Clinic]';
$emailTo = '[email protected]';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$nome = stripslashes(trim($_POST['nome']));
$email = stripslashes(trim($_POST['email']));
$telefone = stripslashes(trim($_POST['telefone']));
$cro = stripslashes(trim($_POST['cro']));
$endereco = stripslashes(trim($_POST['endereco']));
$cidade = stripslashes(trim($_POST['cidade']));
$estado = stripslashes(trim($_POST['estado']));
$mensagem = stripslashes(trim($_POST['mensagem']));
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $nome) || preg_match($pattern, $email) || preg_match($pattern, $subjectPrefix)) {
die("Header injection detected");
}
$emailIsValid = preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email);
if($nome && $email && $emailIsValid && $subjectPrefix && $mensagem){
$subject = "$subjectPrefix";
$body = "<b>Nome:</b> $nome <br />
<b>Email:</b> $email <br />
<b>Telefone:</b> $telefone <br />
<b>CRO:</b> $cro <br />
<b>Endereço:</b> $endereco <br />
<b>Cidade:</b> $cidade <br />
<b>Estado:</b> $estado <br />
<b>Mensagem:</b> $mensagem";
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
$headers .= "From: $nome <$email>" . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
$hasError = true;
}
}
?>
You only have two
if
's in this code, which are the only ones that may be validating something that you don’t want, which exactly you don’t want to validate and is being validated? How difficult is it to edit these conditions? You need to be a little clearer...– Kenny Rafael
I edited the Kenny post. Is it because the form is only sending if all fields are filled in, if not it returns: <? php if(!Empty($hasError)): ? > <div class="Alert Alert-Danger text-center">There was an error in sending, please try again later. </div> <? php endif; ?>
– Marcelo Victor
You have to review your Ifs. Creating a method just to validate the specific field becomes more understandable.
– EmanuelF
Export a
create table
of your table in the database and post in the question, I’m already 99% sure of what the error is...– Kenny Rafael
What do you mean @Kenny Rafael? No database.
– Marcelo Victor
Boy, did I travel pretty...
– Kenny Rafael
@Marcelovictor, there is only one possibility of this variable
$hasError
not to beempty
, if any of the conditions in yourif
are false, try to give avar_dump($nome, $email, $emailIsValid, $subjectPrefix, $mensagem);die;
before theif
and check which one is fake.– Kenny Rafael