Home > Software design >  PHP form does not check other inputs
PHP form does not check other inputs

Time:10-12

I have made a contact form, however the PHP only checks for 'message' and if thats filled out all other checks are ignored. If its not filled out all other checks work as well. Can someone please help me fix my code? Thanks.

<?php
$fullName = $customerEmail =  $message = '';
$errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');

if (isset($_POST['submit'])) {
    $myEmail = "xxxxxxxxxxxxx";
    $timeStamp = date("dd/M/YY HH:i:s");
    $body = "";

    $fullName = $_POST['fullName'];
    $customerEmail = $_POST['customerEmail'];
    $chosenSubject = $_POST['subject'];
    $message = $_POST['message'];

    $body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
    $body .= "Email: " . $customerEmail . "\r\n";
    $body .= "Message: " . $message . "\r\n";

    if (empty($fullName)) {
        $errors['fullName'] = "Full name is required. <br>";
    } else {
        if (!preg_match("/^([a-zA-Z' ] )$/", $fullName)) {
            $errors['fullName'] = "Your name must be a valid name.";
        }
        
    }

    if (empty($customerEmail)) {
        $errors['customerEmail'] = "An email is required. <br>";
    } else {
        if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
            $errors['customerEmail'] = "Email must be a valid email address.";
        }
    }

    if (empty($message)) {
        $errors['message'] = "A message is required.";
    } 
    
    else {
        mail($myEmail, $chosenSubject, $body);
        header("Location: public/mail_sent.php");
    }
}
?>

CodePudding user response:

Last else was changed to an IF check with all the fields, and only if none of them is empty it gets sent.

<?php
$fullName = $customerEmail =  $message = '';
$errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');

if (isset($_POST['submit'])) {
    $myEmail = "[email protected]";
    $timeStamp = date("dd/M/YY HH:i:s");
    $body = "";

    $fullName = $_POST['fullName'];
    $customerEmail = $_POST['customerEmail'];
    $chosenSubject = $_POST['subject'];
    $message = $_POST['message'];

    $body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
    $body .= "Email: " . $customerEmail . "\r\n";
    $body .= "Message: " . $message . "\r\n";

    if (empty($fullName)) {
        $errors['fullName'] = "Full name is required. <br>";
    } else {
        if (!preg_match("/^([a-zA-Z' ] )$/", $fullName)) {
            $errors['fullName'] = "Your name must be a valid name.";
        }
        
    }

    if (empty($customerEmail)) {
        $errors['customerEmail'] = "An email is required. <br>";
    } else {
        if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
            $errors['customerEmail'] = "Email must be a valid email address.";
        }
    }

    if (empty($message)) {
        $errors['message'] = "A message is required.";
    } 
    
    if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {
        mail($myEmail, $chosenSubject, $body);
        header("Location: public/mail_sent.php");
    }
}
?>

CodePudding user response:

Your last else should be changed to IF, checking if any of the fields is not empty then sends the message.

if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {

    mail($myEmail, $chosenSubject, $body);

    header("Location: public/mail_sent.php");
}
  • Related