我正在通过jquery验证表单教程:https://www.youtube.com/watch?v=j7lnU8hJRjo
虽然我没有收到此代码的电子邮件。
我也在使用XAMPP来解决这个问题。我认为这可能是问题,但它也不能在我的实时网站上运行。这是我的代码:
<?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>
答案 0 :(得分:0)
将您的脚本更新为
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
不确定为什么你没有收到这些电子邮件,但我有一个模板供你试用。
$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.