请帮我解码真正的问题。问题是我不断收到一封空白的电子邮件,尽管我对这段代码进行了调整和研究。下面是我的html,javascript(ajax)和php代码。
HTML代码:名为contact.html的文件
<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>Name *</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>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</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">Submit Message</button>
</div>
</div>
</form>
AJAX代码:名为main.js的文件
// Contact 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'),
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();
});
});
PHP代码:名为sendemail.php的文件
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email@email.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
有了这个,我继续收到如下所示的空白电子邮件
Name:
Email: Blank
Phone: Blank
Subject: Blank
Message: Blank
答案 0 :(得分:0)
您是否尝试过在脚本结尾处使用die()?我想你可能会过快地杀死你的页面。
还尝试创建一个这样的捕获:
if (!mail(...))
{
// Reschedule for later try or panic appropriately!
}
此外,您正在使用@ before mail()之前抑制警告; 尝试删除@。
编辑:此外,尝试删除修剪。
答案 1 :(得分:0)
你在调用ajax时没有设置数据,这可能是你的问题。
答案 2 :(得分:0)
当我测试你的代码时,我发现虽然你在form属性中将mehtod定义为POST,但它会触发一个GET请求。 (也许这就是为什么jQuery使用GET作为默认值...)。所以$ _POST ['name']是空的,因为参数在$ _GET ['name']中。
我提交表单时看不到请求参数。 (也许这就是'数据'属性没有设置的原因..)
请尝试使用以下内容完成$ .ajax:
method: 'POST'
data: $(this).serialize()
答案 3 :(得分:0)
非常感谢你们所有人(@Mateusz Klimentowicz,@ Mat VP,@ Jose ManuelAbarcaRodríguez,@ Vladimir Nu)。我实际上按照你的指示,最后纠正了JS文件和PHP文件,如下所示:
JS档案:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData,
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();
});
});
虽然PHP代码如下所示:
<?php
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. As soon as possible we will contact you!'
);
$fail = array(
'type'=>'fail',
'message'=>'Please try again. Your mail could not be delivered!'
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email@mail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
header('Content-type: application/json');
if($success){
echo json_encode($status);
}
else{
echo json_encode($fail);
}
?>
使用下面所述的代码后,我最终没有收到任何空白页面的电子邮件。电子邮件现在已经过了内容。非常感谢。