0

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>
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
roxy-p
  • 35
  • 7
  • Are you getting any error? Seems your constructor missing 4th parameter. – AnkiiG Dec 30 '15 at 12:01
  • Didn't you post an identical message earlier? I posted lots of comments in it. – Barmar Dec 30 '15 at 12:01
  • this is invalid `$subject = 'Website Contact Form Submission'` - You should read the manual on `mail()`. – Funk Forty Niner Dec 30 '15 at 12:33
  • @AG21 - It shows successful and I receive the email no problem. Would you point me in the direction of how to place the 4th parameter? – roxy-p Dec 30 '15 at 14:16
  • @Fred-ii- I adjusted the mail() to now read: mail($to,$subject,$message,$headers); Thanks! – roxy-p Dec 30 '15 at 14:16
  • @roxy-p You're welcome. I take it that it's working then. – Funk Forty Niner Dec 30 '15 at 14:18
  • @Fred-ii The emails send but it still doesn't stop them from sending when there's an error. For example, if I only fill the message box with one character it pops up that there must be at least 5 characters but it still sends the email? Am I missing something in my code? – roxy-p Dec 30 '15 at 14:33

2 Answers2

0

You need to do server side validations before sending mail. For eg. Do email validation to check whether email is in correct format or not. There are several ways to do that:

1: This might help you http://www.w3schools.com/php/php_form_validation.asp 2: You can also change this

 <i class="fa fa-paper-plane"></i><input type="text" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">

to

 <i class="fa fa-paper-plane"></i><input type="email" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">

Hope This helps.

Ankit21ks
  • 468
  • 1
  • 7
  • 22
  • Wow! Thanks so much for this. I'm going to go through the tutorial and try my hand at it. I really appreciate this! – roxy-p Dec 30 '15 at 14:35
0

You already have a required attribute and you dont need the validate functions anymore in your php. Also if your browser supports html5 then you can change this

<input type="text" name="email" to

<input type="email" name="email" pattern="/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i">

Then it will take care of itself.Just check if you have any javascript errors?

Unni
  • 99
  • 1
  • 9