0

I am not a coder but my dev is off tod and I need to fix a clients old site whilst we build their new one.

I have an issue with the contact form where when a valid email is entered, error message "Email is Required" displays, but the form still submits.

When we receive the email, its from "unknown sender" and the body text line "email" is empty.

PHP

    <?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "ned@heyflux.com";
$Subject = "New Message Received";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:" .$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?> 

HTML

        <h2>Get in Touch</h2>

            <form role="form" id="contactForm" class="form-horizontal shake" data-toggle="validator">
                <div class="form-group">
                    <div class="col-md-12">
                        <label for="name" class="h4 sr-only">Name</label>
                        <input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="Enter name">
                        <div class="help-block with-errors"></div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <label for="email" class="h4 sr-only">Email</label>
                        <input type="email" class="form-control" id="email" placeholder="Enter email" required data-error="Enter valid email">
                        <div class="help-block with-errors"></div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <label for="message" class="h4 sr-only">Message</label>
                        <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required data-error="Enter Message"></textarea>
                        <div class="help-block with-errors"></div>
                    </div>
                </div>

                <div class="form-group row-clean">
                    <div class="col-md-12">
                        <button type="submit" id="form-submit" class="btn btn-default m-btn m-btn-orange m-btn-orange-block m-btn-min">Send</button>
                        <div id="msgSubmit" class="h3 hidden"></div>
                    </div>
                </div>
            </form>
        </div>    
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    _one_ problem is, that the inputs don't have `name` properties. That's the reason why the error message shows up. Put a `name='email'` inside the `` – Jeff Jul 20 '17 at 00:52
  • the reason for the mail still beeing sent is just a badly implemented logic: it _first_ sends the email, _then_ checks if something went wrong to display the error. – Jeff Jul 20 '17 at 00:54
  • there should be a `if(!$errorMsg>"") {}` around `$success = mail(....);` – Jeff Jul 20 '17 at 00:56

0 Answers0