du5407 2014-11-16 03:39
浏览 69

电子邮件不通过jquery表单验证发送

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 
";
        $headers .= "Content-type: text/html; charset=iso-8859-1 
";
        $headers .= "From: $name <$email> 
";
        $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>
  • 写回答

1条回答 默认 最新

  • dongreng9864 2014-11-16 03:43
    关注

    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:" . "
    
    " . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "
    
    " . $_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.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入