I am not skilled in forms but I'm using a form that sends an email generated by filling in a contact form.
If a field is not correctly entered into, it shows an error message but still sends the email.
I think I am not validating the form and don't know the code to do so.
I need that if there is an error, it doesn't send the email until the field is correctly filled out.
Thanks!
PHP:
<?php
/*
* Contact Form Class
*/
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$to = 'example@example.com';
$message_min_length = 5; // Min Message Length
$message = "
    <html>
    <head>
    </head>
    <body>
    <table>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["name"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["event_date"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["guests"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["hear_about"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["message"]."</td></tr>
     </table>
    </body>
    </html>
    ";
$headers .= "From: ".$_POST["name"]." <".$_POST["email"].">\r\n";
mail($to,$subject = 'Website Contact Form Submission',$message,$headers);
class Contact_Form{
    function __construct($details, $to, $message_min_length, $email_message){
        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Website Contact Form Submission'; // Subject
        $this->telephone = strip_tags($details['telephone']);
        $this->event_date = strip_tags($details['event_date']);
        $this->guests = strip_tags($details['guests']);
        $this->hear_about = strip_tags($details['hear_about']);
        $this->message = strip_tags($details['message']);
        $this->to = $to;
        $this->message_min_length = $message_min_length;
        $this->response_status = true;
        $this->response_html = '';
    }
    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }
        return empty($string) ? true : false;
    }
    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = false;
        }
        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = false;
        }
        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid email address</p>';
            $this->response_status = false;
        }
        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = false;
        }
    }
    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());
    if($mail)
        {
            $this->response_status = true;
            $this->response_html = '<p>Message Sent. Thank You!</p>';
        }
    }
    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }
        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;
        echo json_encode($response);
    }
}
$contact_form = new Contact_Form($_POST, $to, $message_min_length);
$contact_form->sendRequest();
?>
FORM:
<form name="contact-form" class="contact-form" method="POST" role="form" action="contact_form.php">
    <div class="form-input-group">
        <i class="fa fa-male"></i><input type="text" class="" name="name" required maxlength="80" placeholder="Full name" novalidate="novalidate">
    </div>
    <div class="form-input-group">
        <i class="fa fa-paper-plane"></i><input type="text" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">
    </div>
    <div class="form-input-group">
        <i class="fa fa-phone"></i><input type="text" name="telephone" required maxlength="15" placeholder="Phone number" novalidate="novalidate">
    </div>
    <div class="form-input-group">
        <i class="fa fa-calendar-o"></i><input type="text" name="event_date" required maxlength="30" placeholder="Event date" novalidate="novalidate">
    </div>
    <div class="form-input-group">
        <i class="fa fa-users"></i><input type="text" name="guests" required maxlength="30" placeholder="Guest amount" novalidate="novalidate">
    </div>
    <div class="form-input-group">
        <i class="fa fa-bullhorn"></i><input type="text" name="hear_about" required maxlength="80" placeholder="How did you hear about us?" novalidate="novalidate">
    </div>
    <div class="form-input-group text-container">
        <i class="fa fa-envelope"></i><textarea name="message" class="text" maxlength="1000" cols="25" rows="6" placeholder="Message" required novalidate="novalidate"></textarea>
    </div>
    <button type="submit" class="btn-fill form-btn" id="contact-submit">Submit</button>
    <div id="response"></div>
</form>
 
     
     
    