我目前正在我的个人网站上工作,除了当用户通过联系表单传递消息,并且电子邮件成功发送给我,我已经做了所有事情,但所述电子邮件中包含的内容是空白。
我的代码如下:
PHP(sendmail.php)
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Your email has been succesfully sent.'
);
$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 = 'me@joshuaquinlan.co.uk';
$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;
HTML(contact-us.html):
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendmail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
然而,当实际发送电子邮件时,它实际上显示为 this
如果有人可以帮助我,请谢谢!
答案 0 :(得分:1)
您的问题是没有从表单中发送任何内容:
$.post($(this).attr('action'),function(data){$this.prev().text(data.message) // ...
您忘了添加数据:
$.post($(this).attr('action'),$(this).serialize(), function(data){$this.prev().text(data.message) // ...
美化代码
// You forgot to add data here v----
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
所以纠正它:
// added data using serialize() vvvvvvvvvvvvvvvvvv
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
查找和替换
$.post($(this).attr('action'),func
。$.post($(this).attr('action'),$(this).serialize(),func
。<强>截图强>