0
我通過JQuery驗證形式的教程在這裏工作發出:https://www.youtube.com/watch?v=j7lnU8hJRjo電子郵件不使用jQuery表單驗證
雖然,我不是從這個代碼接收電子郵件。
我也在用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 = "[email protected]";
$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