我使用此脚本从联系表单发送电子邮件。我收到了电子邮件,但信息是空的,我试着回复"邮政价值但它不会,我错过了什么? 我认为我的HTML表单可能有问题,但我无法看到它。
sendemail.php
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$nome = addslashes($_POST['name']);
$email = addslashes($_POST['email']);
$tel = addslashes($_POST['telephone']);
$subject = addslashes($_POST['subject']);
$msg = addslashes($_POST['message']);
$para = "*******@gmail.com";
$body = "Nome: " .$nome. "\nE-mail: " .$email. "\nTelefone: " .$tel. "\nMensagem: " .$msg;
$header = "From: contact@*******.com.br"."\r\n"."Reply-To: ".$email."\r\n"."X-Mailer: PHP/".phpversion();
$success = mail($para, $subject, $body, $header);
echo json_encode($status);
表单页面html
<section id="contact-page">
<div class="container">
<div class="center">
<h2>Deixe sua mensagem</h2>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Nome *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Telefone</label>
<input type="phone" name="telephone"class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Assunto *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Mensagem *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Enviar</button>
</div>
</div>
</form>
</div>
</div>
</section>
JS中的AJAX:
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
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">' + data.message + '</p>').delay(3000).fadeOut();
});
});
答案 0 :(得分:0)
默认情况下,会发送ajax via the GET method。此JavaScript代码未指定POST方法;因此虽然调用了PHP脚本,但它从未收到任何数据。
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
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">' + data.message + '</p>').delay(3000).fadeOut();
});
});
更新的脚本看起来像这样(参见jQuery AJAX submit form):
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
type: "POST",
data: form.serialize(), // serializes the form's elements.
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">' + data.message + '</p>').delay(3000).fadeOut();
});
});