Why am i getting undefined variable error after POST in my PHP contact script?

258 Views Asked by At

I am trying to iron out a bug in my contact form. I am getting undefined variable $errEmail on line 24 and $errMessage on line 22. Both variables are defined, to my understanding. My contact form works other than these errors. The errors occur after a user submits the contact form. Below is my code. I just changed the $to to [email protected] to hide my email.

<?php
    
if (isset($_POST["submit"])) {
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Contact Form';
    $to = '[email protected]';
    $subject = 'Message from Contact';

    $body = " E-Mail: $email\n Message:\n $message";

    // Check if email has been entered and is valid
    if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    // Check if message has been entered
    if (empty($message)) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}
?>

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1 class="page-header text-center">Contact Us</h1>
            <form class="form-horizontal" role="form" method="post">
                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]">
                        <?php if (isset($errEmail)) {
                            echo "<p class='text-danger'>$errEmail</p>";
                        }?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="message" class="col-sm-2 control-label">Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="message"></textarea>

                        <?php if (isset($errMessage)) {
                            echo "<p class='text-danger'>$errMessage</p>";
                        }?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php if (isset($result)) {
                            echo $result;
                        } ?>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>

I have reviewed lines 22 and see that the variable is defined on lines 13 and 18.

1

There are 1 best solutions below

2
Karl Hill On BEST ANSWER

You should initialize $errEmail and $errMessage to an empty string before the if conditions. This ensures that these variables are always defined, regardless of whether the if conditions are met.

$errEmail = '';
$errMessage = '';

if (isset($_POST["submit"])) {
    // Your existing code...

    // If there are no errors, send the email
    if ($errEmail == '' && $errMessage == '') {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}