我有一个发送电子邮件的基本表单。它正在发送电子邮件,但不传递表单中的数据。是否有我缺少的东西允许数据发布?
用于发送电子邮件的HTML表单:
<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 name="name" type="text" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input name="email" type="text" class="form-control" required="required" placeholder="Email address">
</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" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
</form>
sendemail.php中的PHP代码
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = "Example Contact";
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'admin@example.com';
$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;
更新: 当class =&#34; contact-form&#34;时,看起来js / jquery中存在问题。已经包括了。如果我将其遗漏,则会发送变量。
以下是表单的jquery代码:
//contact form
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
以下是一些相关帖子:
PHP Blank Email
Contact form won't submit data
js deleting submitted form data
PHP script sending mails but not showing form data
Post Data is not coming
答案 0 :(得分:1)
您正在呼叫jquery.post()
,但您只能说明要发布到哪里以及成功时该怎么做。你发布什么?第二个参数需要是您发布的数据:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');