我有一个使用event.preventdefault的表单。然后我使用Ajax启动脚本工作正常。然后,我使用PHP发送一封电子邮件,除了我没有从我的表单中插入任何值到电子邮件之外,它也可以正常工作 - 任何想法为什么?电子邮件仍然只发送硬编码消息。
形式:
<form action="post.php" class="enquire-form" method="post">
<label for="" class="darktext">Contact details</label>
<div class="form-group">
<input class="form-control" type="text" name="name" id="name" placeholder="NAME" min="2" maxlength="75" required>
</div>
<div class="form-group">
<input class="form-control" type="number" name="contact-number" id="contact-no" placeholder="PHONE NUMBER" min="6" maxlength="20" required>
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" id="email" placeholder="EMAIL ADDRESS" maxlength="40" min="4" required>
</div>
<label for="" class="darktext">Job details</label>
<div class="form-group ">
<input class="form-control" type="text" name="job-title" id="job-title" value="" placeholder="TYPE OF JOB" maxlength="300" min="5" required>
</div>
<div class="form-group ">
<input class="form-control" type="text" name="job-location" id="job-location" value="" placeholder="JOB LOCATION" maxlength="75" required>
</div>
<div class="g-recaptcha" data-sitekey="REMOVED"></div>
<button type="submit" class="submit-job signbuttons btn btn-primary">
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
Send enquiry
</button>
PHP
$name = $_POST['#name'];
$number = $_POST['#contact-no'];
$email = $_POST['#email'];
$jobTitle = $_POST['#job-title'];
$jobLocation = $_POST['#job-location'];
$message = "Job request: \n Name: $name \n Contact Number: $number \n Email:
$email \n Job Title: $jobTitle \n Job location: $jobLocation";
$to = 'me@example.com';
$subject = "Enquiry from $name";
$headers = 'From: me@ex.com' . "\r\n" .
'Reply-To: me@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
的Ajax:
$('.enquire-form').submit(function(e) {
e.preventDefault();
$this = $(this);
$.ajax({
type: "POST",
url: "post.php",
data: $this.serialize()
}).done(function(data) {
alert("Email sent, we'll be in touch soon.");
$(".submit-job").attr("disabled", true).prop('title', 'Your request has already been sent');
}).fail(function( jqXHR, textStatus ) {
alert("Failed to send the request: " + textStatus);
});
});
由于