我不是一名程序员,但是我的开发工作已经完成,我需要修建客户旧网站,同时我们建立新的网站。
我的联系表单存在问题,当输入有效的电子邮件时,错误消息"需要电子邮件"显示,但表单仍然提交。
当我们收到电子邮件时,来自"未知发件人"和正文文本行"电子邮件"是空的。
PHP
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "ned@heyflux.com";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:" .$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
HTML
<h2>Get in Touch</h2>
<form role="form" id="contactForm" class="form-horizontal shake" data-toggle="validator">
<div class="form-group">
<div class="col-md-12">
<label for="name" class="h4 sr-only">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="Enter name">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="h4 sr-only">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" required data-error="Enter valid email">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="h4 sr-only">Message</label>
<textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required data-error="Enter Message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group row-clean">
<div class="col-md-12">
<button type="submit" id="form-submit" class="btn btn-default m-btn m-btn-orange m-btn-orange-block m-btn-min">Send</button>
<div id="msgSubmit" class="h3 hidden"></div>
</div>
</div>
</form>
</div>