Make the form send if only certain fields are filled in

Asked

Viewed 83 times

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...

  • 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; ?>

  • You have to review your Ifs. Creating a method just to validate the specific field becomes more understandable.

  • 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...

  • What do you mean @Kenny Rafael? No database.

  • Boy, did I travel pretty...

  • @Marcelovictor, there is only one possibility of this variable $hasError not to be empty, if any of the conditions in your if are false, try to give a var_dump($nome, $email, $emailIsValid, $subjectPrefix, $mensagem);die; before the if and check which one is fake.

Show 2 more comments

1 answer

1


I think what you want is this:

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

...

For only if the fields $nome , $email and $telefone are empty the form will not be submitted. That is, are mandatory fields.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.