Only if I Echo the variable Result my if works - PHP

Viewed 20

I am trying to validate a contact form but If I don't echo my $result variable my if conditions won't work, only if I will echo it... I can't understand why so if you could help me I will appreciate it, thanks! For more info, you can tell me.

My Code in PHP:

if (isset($_POST['submit'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['phone-number'];
    $date = $_POST['date'];
    $message = $_POST['message'];

    //Sanitizing
    $name =  htmlspecialchars($name);
    $email = htmlspecialchars($email);
    $number = htmlspecialchars($number);
    $date = htmlspecialchars($date);
    $message = htmlspecialchars($message);

    $result = false;

    if(!$name || !$email || !$number || !$date || !$message){
        $result = false;
        header('Location: ../index.php?emptyInputs');
    } else{
        $result = true;
    }
  
    //If email is valid
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $result = false;
        header('Location: ../index.php?mailInvalid');
    }else{
        $result = true;
    }

    if($result){
        //PHPMailer Stuff....
    }
0 Answers
Related