我正在尝试为网站创建联系表单,但它没有正确处理错误。当输入无效的电子邮件地址或字段留空时,它仍在尝试发送电子邮件而不是显示错误消息。我不确定我需要做些什么来解决这个问题。如果没有错误发生,它应该发送联系表格并发送确认电子邮件到表格中列出的地址。
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Leah Stephens</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,700,900,100italic,300italic,400italic,700italic,900italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="../css/contact-styles.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<section id="hire">
<h1>Contact Me</h1>
<form action="mail.php" method="POST">
<div class="field name-box">
<input type="text" name="name" id="name" placeholder="Who Are You?"/>
<label for="name">Name</label>
<?php if(isset($errors['name'])) { echo $errors['name']; } ?>
</div>
<div class="field email-box">
<input type="text" name="email" id="email" placeholder="name@email.com"/>
<label for="email">Email</label>
<?php if(isset($errors['email'])) { echo $errors['email']; } ?>
</div>
<div class="field msg-box">
<textarea name="message" id="msg" rows="4" placeholder="Your message goes here..."/></textarea>
<label for="msg">Msg</label>
<?php if(isset($errors['message'])) { echo $errors['message']; } ?>
</div>
<div class="g-recaptcha" data-sitekey="6Lc3YCQUAAAAAFHsT-Zh6UbHaYebDKeKx_Ywr8qb
"></div>
<br>
<input class="button" type="submit" name="submit" value="Send"/>
</form>
</section>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="../js/contact-js.js"></script>
</body>
</html>
$myEmail = "example@email.net";
$errors=[];
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!isset($_POST['name']) || empty($_POST['name'])){
$error['name'] = 'Please enter your name';
}
if (!isset($_POST['email']) || empty($_POST['email']) || !filter_var($email, FILTER_VALIDATE_EMAIL)){
$error['email'] = 'Please enter a valid email address';
}
if (!isset($_POST['message']) || empty($_POST['message'])){
$error['message'] = 'Please enter your message';
}
if(empty($errors)){
$to = $myEmail;
$subject = "Contact Form Submission: $name";
$body = "From: $name\n Email: $email\n Message:\n $message";
$header = "From: $myEmail\n";
$header .= "Reply-To: $email";
$confirmSubject = "Contact Form Confirmation";
$confirmBody = "Hi!\n Thank you for reaching out. I\'ll do my best to get back to you within the next 48 hours. Have a great day!";
$confirmHeader = "From: $email\n";
$confirmHeader .= "Reply-To: $myEmail";
mail($to, $subject, $body);
mail($email, $confirmSubject, $confirmBody);
header('Location: ../index.html');
}
}
答案 0 :(得分:3)
在使用它们之前,请注意您尚未初始化 $ error ['email'] 或其他类似变量。您将在下一页中稍后对其进行初始化。您肯定不会收到您想要显示的错误消息。
解决方案可能是,而不是将您的表单链接到mail.php,设置 action =“”并在原始页面上编写mail.php的php代码。
并且,检查 $ error 拼写,您拼写了 $ errors