Make the form send if only certain fields are filled

1

When I submit the form it is checking if all fields have been filled out. I want only the name, email and phone to be required 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;
    }
}
?>
    
asked by anonymous 24.10.2016 / 17:41

1 answer

1

I think what you want is this:

if($nome != "" && $email != "" && $emailIsValid && $telefone != ""){

...

Only if the fields $nome , $email and $telefone are empty the form will not be submitted. That is, they are required fields.

    
24.10.2016 / 18:54