以下是我的contact.php
表单以及网站上表单代码的片段。问题是没有从表格发送任何内容到我的电子邮件。并且针对空白条目的默认验证不起作用。这是GoDaddy上托管的appstrap网站。
我不确定为什么什么都没提交到我的电子邮件中。有想法吗?
contact.php
<?php
/**
* PHP Contact form
*
* Add basic form validation: required fields & email address valid
* Sends email to
*/
$email_to = 'info@swiftpros.com'; // update to your email
$email_to_name = 'info@swiftpros.com'; // update to your name
$form_message = '<p>Please use the form below to contact us.</p>';
$form_errors = array();
$form_values = $_POST; // Optionally change to $_GET & <form action="contact.php" method="get"> on form
$required_fields = array( // if any of these fields are blank are error will show
'contact-name' => 'Name',
'contact-email' => 'Email',
'contact-message' => 'Message',
);
if (!empty($form_values)) {
// Form was submitted, validate required fields
if (!empty($required_fields)) {
foreach ($required_fields as $required => $label) {
if (empty($form_values[$required])) {
$form_errors[$required] = "The $label field is required and cannot be left blank."; //Message to show use
}
}
}
if (!empty($form_values['contact-email'])) {
// Email address submitted, validate it
// Remove all illegal characters from email
$email = filter_var($form_values['contact-email'], FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
// Valid, send email
require 'assets/plugins/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom($email, $form_values['contact-name']);
$mail->addAddress($email_to, $email_name);
$mail->Subject = 'Contact form enquiry from ' . $form_values['contact-name'];
$email_body = array();
$email_body[] = '<strong>Email:</strong> ' . $form_values['contact-email'] .'<br />';
$email_body[] = '<strong>Name:</strong> ' . $form_values['contact-name'] .'<br />';
$email_body[] = '<strong>Message:</strong> ' . $form_values['contact-message'] .'<br />';
$email_body[] = 'Email sent at '. date('r', time());
$mail->msgHTML(implode('<br />', $email_body));
if (!$mail->send()) {
$form_errors[] = 'Error in sendng enquiry please email us at ' . $email_to . ' instead.';
}
else {
$form_message = '<div class="alert alert-success" role="alert"><strong>Enquiry Successfully sent!</strong> Thank you we will get back to you as soon as possible.</div>';
}
}
else {
// Invalid
$form_errors['contact-email'] = 'The email address inputted is invalid.';
}
}
// Make errors into message to user
if (!empty($form_errors)) {
$form_message = '<div class="alert alert-danger" role="alert"><strong>Error!</strong> The following errors have occurred submitting the form: <hr />' . implode('<br />', $form_errors) .'</div>';
}
}
?>
HTML表单
<form id="contact-form" action="contact.php" role="form" method="POST">
<div class="form-group" data-animate="fadeInDown" data-animate-delay="0.2">
<label class="sr-only" for="contact-name">Name</label>
<input type="text" class="form-control" id="contact-name" placeholder="Name">
</div>
<div class="form-group" data-animate="fadeInDown" data-animate-delay="0.3">
<label class="sr-only" for="contact-email">Email</label>
<input type="email" class="form-control" id="contact-email" placeholder="Email">
</div>
<div class="form-group" data-animate="fadeInDown" data-animate-delay="0.4">
<label class="sr-only" for="contact-message">Message</label>
<textarea rows="5" class="form-control" id="contact-message" placeholder="Message"></textarea>
</div>
<input type="submit" class="btn btn-outline-primary btn-lg" value="Send Message">
</form>
答案 0 :(得分:1)
在HTML表单中,您无需设置输入名称,而只是将它们分配给ID,例如:
你有
<input type="text" class="form-control" id="contact-name" placeholder="Name">
缺少一个属性名称,这是PHP将用于获取输入值的名称,因此,当您的代码执行if (!empty($form_values['contact-email']))
时,它将返回false,因为您没有名称为{{ 1}},请尝试以下表单,它应该可以工作。
contact-email