我一直在论坛上。这不起作用的一般原因是您需要NAME属性,而不是ID。我有一个名称属性,我仍然在提交时获得空的$ _POST字段。我是一名初学者,已经在这里工作了6个小时试图解决它。这可能是一件简单的事情。请帮忙。
我在ShapeBootstrap上获得了这个免费的模板:https://shapebootstrap.net/item/1524963-evento-free-music-event-template
我的HTML:
<form action="sendemail.php" method="post" name="contact-form" class="contact-form" id="main-contact-form">
<div class="form-group">
<input name="name" type="text" id="name" required="required" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control" required="required" placeholder="Email ID">
</div>
<div class="form-group">
<textarea name="message" id="message" required class="form-control" rows="10" placeholder="Enter your message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right">Send</button>
</div>
<div> </div>
</form>
我的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 = ($_POST["name"]);
$email = ($_POST['email']);
$subject = "Website Message";
$message = ($_POST['message']);
$email_from = $email;
$email_to = 'adam.wilson45@yahoo.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;
JavaScript的:
// Contact form validation
var form = $('.contact-form');
form.submit(function () {'use strict',
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
$( window ).resize(function() {
menuToggle();
});
$('.main-nav ul').onePageNav({
currentClass: 'active',
changeHash: false,
scrollSpeed: 900,
scrollOffset: 0,
scrollThreshold: 0.3,
filter: ':not(.no-scroll)'
});
});
非常感谢先进!
答案 0 :(得分:0)
您没有在$.post
脚本中传递任何数据。
将$.post($(this).attr('action'), function(data) {
更改为$.post($(this).attr('action'), form.serialize(), function(data) {
。 form.serialize()
位将表单数据的对象传递给PHP脚本。
有关详细信息,请参阅http://api.jquery.com/jquery.post/。
答案 1 :(得分:-1)
谢谢@putvande!你的回答指出了我正确的方向!添加form.serialize很接近。我们需要添加$ this。就在它之前。我希望我8小时前跟你说过话。 回答: 改变
$.post($(this).attr('action'), function(data) {
到
$.post($(this).attr('action'), $this.serialize(), function(data) {