我收到了网页的下面部分和.js文件,以便从联系表单中实现邮件功能,但我无法弄清楚。这是表单代码:
<form class="contact-form-wrapper" name="contactForm" id='contact_form' method="post" action='assets/php/mail.php'>
<div class="row">
<div class="form-group col-md-6">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group col-md-6">
<input type="email" class="form-control input-email fill-this" name="email" placeholder="Email">
</div>
<div class="form-group col-md-12">
<input type="text" class="form-control" name="subject" placeholder="Subject">
</div>
<div class="form-group col-md-12">
<textarea class="form-control" name="message" rows="3" placeholder="Message"></textarea>
</div>
<div class="form-group col-md-12">
<div id="mail_success" class="success" style="display:none;">
Thank You ! Your email has been delivered.
</div>
<div id="mail_fail" class="error" style="display:none;">
An error occured, please try again later !
</div>
</div>
<div class="form-group col-md-12" id="submit">
<input class="btn btn-md btn-dark" type="submit" id="send_message" value="Submit">
</div>
</div>
</form
这是js文件:
$(document).ready(function(){
$('#send_message').click(function(e){
// Stop form submission & check the validation
e.preventDefault();
// Variable declaration
var error = false;
var email = $('.input-email').val();
var required = $('.fill-this').val();
if(email.length == 0 || email.indexOf('@') == '-1'){
var error = true;
// $('#email_error').fadeIn(500);
$('.input-email').addClass("validation");
}else{
$('.input-email').removeClass("validation");
}
if(required.length == 0){
var error = true;
$('.fill-this').addClass("validation");
}else{
$('.fill-this').removeClass("validation");
}
// If there is no validation error, next to process the mail function
if(error == false){
// Disable submit button just after the form processed 1st time successfully.
$('#send_message').attr({'enabled' : 'enable', 'value' : 'Sending...' });
$message = "wrong answer";
/* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/
$.post("../php/email.php", $("#contact_form").serialize(),function(result){
//Check the result set from email.php file.
if(result == 'sent'){
//If the email is sent successfully, remove the submit button
$message = "wrong answer";
$('#send_message').addClass("hidden").attr({'enabled' : 'enable', 'value' : 'send' });
//Display the success message
$('#mail_success').fadeIn(500);
}else{
//Display the error message
$message = "wrong answer";
$('#mail_fail').fadeIn(500);
// Enable the submit button again
$('#send_message').removeAttr('disabled').attr('value', 'Send The Message');
}
});
}
});
});
我在谷歌的帮助下创建了一个mail.php文件,这就是我现在所拥有的:
<?php
$errors = '';
$myemail = 'darren@prawno.fr';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
header('Location: index.html');
}
?>
整个事情似乎都不起作用。在我填写表单并点击发送后,按钮显示发送..&#39;根据js文件但实际上没有发生任何事情。我只使用了不涉及任何js或ajax的简单联系表单。任何帮助,将不胜感激。谢谢!