PHP 表单 post 由于 jQuery 而未收到数据
PHP form post data not being received due to jQuery
我有一个发送电子邮件的基本表格。它发送电子邮件,但不传递表单中的数据。我是否遗漏了允许数据 POST 的内容?
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>
PHP sendemail.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="contact-form" 时,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
您正在调用 jquery.post()
,但您只说 post 去哪里,以及成功后要做什么。你post在干什么?第二个参数需要是您正在 posting 的数据:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
我有一个发送电子邮件的基本表格。它发送电子邮件,但不传递表单中的数据。我是否遗漏了允许数据 POST 的内容?
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>
PHP sendemail.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="contact-form" 时,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
您正在调用 jquery.post()
,但您只说 post 去哪里,以及成功后要做什么。你post在干什么?第二个参数需要是您正在 posting 的数据:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');