好的,所以当我谈到这一切时,我有点像菜鸟......但是我现在处于突破点,试图让它在今天运行近10个小时,我所尝试的一切似乎都失败了,所以任何建议都会受到赞赏。
电子邮件发送正常,但是到达时它们只显示:
名称:
姓:
电子邮件:
消息:
我还没有能够获得除此之外的其他任何东西。
我的PHP代码是:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = $_POST['name'];
$surname = $_POST[‘surname’];
$email = $_POST[‘email’];
$message = $_POST[‘message’];
$subject = "Contact Form Submission";
$email_from = $email;
$email_to = 'admin@park.co.uk';
$body = 'Name: ' . $name . "\n\n" . 'Surname: ' . $surname . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Main JS编码如下:
//contact form
var form = $(‘.contact-form’);
form.submit(function () {
$this = $(this);
$.post($(this).attr(‘action’),$(‘.contact-form’).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
$this.closest(‘.contact-form’).find(“input[type=text], input[type=email], textarea”).val(“”);
},’json’);
return false;
});
//goto top
$(‘.gototop’).click(function(event) {
event.preventDefault();
$(‘html, body’).animate({
scrollTop: $(“body”).offset().top
}, 500);
});
HTML代码是:
<h4>Contact Form</h4>
<form id="main-contact-form" class="contact-form" name="contact-form"
method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group"> <input class="form-control" required="required"
placeholder="Name" type="text"> </div>
<div class="form-group"> <input class="form-control" required="required"
placeholder="Surname" type="text"> </div>
<div class="form-group"> <input class="form-control" required="required"
placeholder="Email" type="text"> </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>
<input name="subject" value="Form submission" type="hidden"> </div>
</div>
</form>
</div>
如果有人能帮助我完成这项工作,我会非常感激,我的理智也是如此!
提前致谢。
答案 0 :(得分:0)
很难确定造成问题的原因,但有几个问题,包括;
serialize()
执行此任务所需的名称属性它是我,我会使用ajax来实现这个目标:
<强> HTML:强>
<div class="row">
<h4>Contact Form</h4>
<div id="data"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input class="form-control" required="required" placeholder="Email to send to.. (Added for the demo)" name="to" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Name" name="name" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Surname" name="surname" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Email" name="email" type="text">
</div>
<div class="form-group">
<textarea required class="form-control" rows="8" placeholder="Message" name="message"></textarea>
<input name="subject" value="Form submission" type="hidden">
</div>
</div>
</div>
<div class="form-group">
<button id="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</form>
</div>
<强> JavaScript的:强>
$(function() {
//contact form
$('#main-contact-form').submit(function(event) {
event.preventDefault(); // stop the normal form submission
var sendVars = $(this).serialize();
$.ajax({
type: "POST",
url: "sendemail.php",
data: sendVars,
dataType: 'json',
success: function(data) {
// do stuff...
console.log(data);
$('#data').text(data.message).fadeIn().delay(3000).fadeOut();
$('#main-contact-form').find('input, textarea').val('');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
//goto top
$('.gototop').click(function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $('body').offset().top
}, 500);
});
});
<强> PHP 强>
<?php
//ini_set('display_errors',1); //error reporting if needed
//ini_set('display_startup_errors',1);
//error_reporting(-1);
header('Content-type: application/json');
$status = array( 'fail'=>array('type'=>'failure',
'message'=>'Email Failed!'),
'succeed'=>array('type'=>'success',
'message'=>'Email Sent!'));
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']): null;
$surname = isset($_POST['surname']) ? htmlspecialchars($_POST['surname']): null;
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']): null;
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']): null;
$subject = "Contact Form Submission";
$email_from = $email;
$email_to = isset($_POST['message']) ? htmlspecialchars($_POST['to']): null;
$body = 'Name: ' . $name . "\n\n" . 'Surname: ' . $surname . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;
if(mail($email_to, $subject, $body, 'From: <'.$email_from.'>')){
echo json_encode($status['succeed']);
die;
}
else{
echo json_encode($status['fail']);
}
?>