我最近一直在使用模板构建一个网站,一切都很好......除了电子邮件脚本变量每次都是空白的。
表格:
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
动作(sendemail.php):
<?php
$name = @trim(stripslashes($_POST['name']));
$from = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$to = 'info@website.org';
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1"."\r\n";
$headers .= "From: ".$name." <".$from.">"."\r\n";
$headers .= "Reply-To: <".$from.">"."\r\n";
$headers .= "Subject: ".$subject."\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
?>
jQuery:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: "POST",
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
你们有什么见解吗?我通常不会搞乱jQuery。
答案 0 :(得分:1)
对于PHP:
$headers = [ // or array( for older PHP versions.
"MIME-Version: 1.0",
"Content-type: text/plain; charset=iso-8859-1",
"From: $name <$from>",
"Reply-To: <$from>",
"Subject: $subject",
"X-Mailer: PHP/".phpversion()
];
mail($to, $subject, $message, join('\r\n', $headers));
对于Javascript:
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: {
name: $('#name').val(),
email: $('#email').val(),
subject: $('#subject').val(),
message: $('#message').val()
},
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
对于HTML
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" id="name" class="form-control" required="required" placeholder="Name"></input>
</div>
<div class="form-group">
<input type="email" id="email" class="form-control" required="required" placeholder="Email Id"></input>
</div>
<div class="form-group">
<input type="text" id="subject" class="form-control" required="required" placeholder="Subject"></input>
</div>
<div class="form-group">
<textarea id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit"></input>
</div>
</form>