我不知道因为这个而丢失了多少工作......但我的表格无法正常工作。
当我收到电子邮件时,它们完全是空白的。我的代码如下:
<span class="errmsg"></span>
<label for="contact-name">Name</label>
<input type="text" id="contact-name" name="contact-name" required>
<label for="contact-email">E-mail address</label>
<input type="email" id="contact-email" name="contact-email" required>
<label for="contact-subject">Subject</label>
<input type="text" id="contact-subject" name="contact-subject" required>
<label for="contact-message">Message</label>
<textarea id="contact-message" name="contact-message" required></textarea>
<div class="button" type="submit" id="contact-submit" name="contact-submit">Submit</div>
我没有选择经典的<form>...</form>
构造,而是选择使用ajax帖子将数据发送到PHP:
$("#contact-submit").click(function() {
// validateForm() just checks that required fields have values
if (validateForm()) {
$.ajax({
type: 'POST',
url: '/contact.php',
data: { name: $('#contact-name').val(),
from: $('#contact-email').val(),
subject: $('#contact-subject').val(),
message: $('#contact-message').val()
}, // end data
success: function clearFields() {
$('#contact-name').val('');
$('#contact-email').val('');
$('#contact-subject').val('');
$('#contact-message').val('');
$('.errmsg').text('Your email was sent successfully.');
$('.errmsg').css('color', '#389320');
} // end success
}); // end ajax
}
else
{
var errmsg = "Your email could not be sent.<br />";
errmsg += "Please ensure that you've filled in all the fields.";
$(".errmsg").html(errmsg);
$(".errmsg").css("color", "#ff0000");
}
}); // end click
这(应该)将表单数据发布到PHP文件下面,该文件发送电子邮件:
<?php
error_reporting(E_ALL);
require_once("class.phpmailer.php");
include("class.smtp.php");
$email = new PHPMailer();
try
{
//
// Set server details for send
//
$email->IsSMTP();
$email->Host = "mail.this.com";
$email->Port = 25;
$email->SMTPAuth = true;
$email->Username = "info@this.com";
$email->Password = "p455W0rd";
//
// Send mail from the contact form
//
$to = "info@this.com";
$from = $_POST["contact-email"];
$name = $_POST["contact-name"];
$subject = "From web: ".$_POST["contact-subject"];
$message = $_POST["contact-message"];
$body = "<p>Hi Ortund,</p>";
$body .= "<p>You have received a new query on your website.</p><p>Please see below:</p>";
$body .= "<p>";
$body .= str_replace("\r\n", "<br />", $message);
$body .= "</p>";
$body .= "</body></html>";
$email->SetFrom($from, $name);
$email->AddReplyTo($from, $name);
$email->AddAddress($to, $to);
$email->Subject = $subject;
$email->Body = $body;
$email->IsHTML(true);
$email->Send();
session_start();
$_SESSION["mailresult"] = "success";
http_redirect("http://www.this.com");
}
catch (Exception $e)
{
$_SESSION["mailresult"] = "failed";
$_SESSION["mailerror"] = $e->getMessage();
}
?>
有几层检查可以向用户返回一条消息,告知用户发生了什么,但没有任何工作,这对我来说也是一个主要问题......
现在我很乐意将电子邮件表格数据收录到电子邮件中,但我无法理解为什么它不存在...
如果有人可以帮助我,我真的很感激。
编辑
以下是我现在收到的电子邮件示例:
来自网络:
root用户:
发送时间:星期一2014/05/26 12:24 PM
致:info@this.com嗨Ortund,
您的网站上收到了新的查询。
请参阅以下内容:
答案 0 :(得分:2)
使用此名称
发布数据 data: { name: $('#contact-name').val(),
from: $('#contact-email').val(),
subject: $('#contact-subject').val(),
message: $('#contact-message').val()
},
但在此处使用其他名称检索
$from = $_POST["contact-email"];
$name = $_POST["contact-name"];
$subject = "From web: ".$_POST["contact-subject"];
$message = $_POST["contact-message"];
所以像这样使用
$from = $_POST["from"];
$name = $_POST["name"];
$subject = "From web: ".$_POST["subject"];
$message = $_POST["message"];