0

I'm working through a jquery validate form tutorial here: https://www.youtube.com/watch?v=j7lnU8hJRjo

ALthough, I am not receiving emails from this code.

I am also using XAMPP to trouble shoot this. I thought that might be the problem but it also doesn't work on my live website. Here is my code:

<?php

$hasError = false;
$sent = false;

if(isset($_POST['submitform'])) {
    $name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
    $email = trim($_POST['email']);
    $message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));

$fieldsArray = array(
    'name' => $name,
    'email' => $email,
    'message' => $message
);

    $errorArray = array();

    foreach($fieldsArray as $key => $val) {
        switch ($key) {
            case 'name':
            case 'message':
                if(empty($val)) {
                    $hasError = true;
                    $errorArray[$key] = ucfirst($key) . " field was left empty.";
                }
                break;
            case 'email':
                if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $hasError = true;
                    $errorArray[$key] = "Invalid Email Address Entered";
                } else {
                    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
                }
                break;
        }
    }

    if($hasError !== true) {
        $to = "myemail@myemail.com";
        $subject = "Message from Contact Form";
        $msgcontents = "Name: $name<br>Email: $email<br>Message: $message";
        $headers = "MIME-VERSION: 1.0 \r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
        $headers .= "From: $name <$email> \r\n";
        $mailsent = mail($to, $subject, $msgcontents, $headers);
        if($mailsent) {
            $sent = true;
            unset($name);
            unset($email);
            unset($message);
        }
    }
}

?>

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="style.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>


<script type = "text/javascript">
jQuery(document).ready(function($) {
    $("#contactform").validate({
        rules: {
            name: {
                required: true,
                minlength:2
            },
            email: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 10
            }
        },
        messages: {
            name: {
                required: "Please enter your name",
                minlength: "Your name is too short"
            },
            email: {
                required: "Please enter your email address",
                email: "Please enter a valid email address"
            },
            message: {
                required: "Please enter your message",
                minlength: "Your message is too short"
            }
        }
    });
});
</script>

</head>
<body>

<div id="main">
<div id="centercol">

<form id="contactform" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" novalidate> 
   <?php
       if($sent === true) {
           echo "<h2 class = 'success'>Thanks, your message has been sent to the fine folks ...</h2>";
       } elseif($hasError === true) {
             echo '<ul class= "errorlist">';
             foreach($errorArray as $key => $val) {
                 echo "<li>" . ucfirst($key) . " field error - $val</li>";
             }
             echo '</ul>';
       }
   ?>

<input type="text" name="name" value="<?php echo (isset($name) ? $name : ""); ?>" placeholder="Your Name">
<input type="email" name="email" value="<?php echo (isset($email) ? $email : ""); ?>" placeholder="Your Email">
<textarea name="message" placeholder="Your Message"><?php echo (isset($message) ? $message : "") ?></textarea>
<input type="submit" name="submitform" value="send">
</form>


</div>
</div>

</body>
</html>
Brandon
  • 1
  • 2

1 Answers1

0

update your script to

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

not sure why your arent receiving the emails but here is a template i made for you to try.

      $to = "ur email@example.edu"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];  
$subject2 = "Copy of your form submission";
$message = $first_name . " " ." wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
// You can also use header('Location: thank_you.php'); to redirect to another page.
Dino Bic Boi
  • 49
  • 10