我在这方面并不精通,其他主题并没有真正帮助我。我尝试使用不同的部分来编辑表单的PHP处理程序,但它不起作用。
我在用户单击“提交”后收到了电子邮件,但是该电子邮件为空。 另外,我没有对尝试注入项目的用户进行验证。
有人可以帮我添加一些验证,并将信息显示在电子邮件中吗?
感谢您的帮助
已解决2号编辑!::::: 现在让PHP和JS交流的很好。一切正常。 JS代码中的PHP文件名错误。
<?php
$errors = '';
$myemail = 'MYADDRESS';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$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 submission from your website. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phone \n Message: \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
// header('Location: index.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
// JavaScript contact form Document
$(document).ready(function() {
$('form#contact-form').submit(function() {
$('form#contact-form .error').remove();
var hasError = false;
$('.requiredField').each(function() {
if(jQuery.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'</span>');
$(this).addClass('inputError');
hasError = true;
} else if($(this).hasClass('email')) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You entered an invalid '+labelText+'</span>');
$(this).addClass('inputError');
hasError = true;
}
}
});
if(!hasError) {
$('form#contact-form input.submit').fadeOut('normal', function() {
$(this).parent().append('');
});
$("#loader").show();
$.ajax({
url: "contact-form-handler.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$('form#contact-form').slideUp("fast", function() { $(this).before('<div class="col-lg-7 mx-auto success-box success"><img class="img-fluid" src="assets/img/support.png" alt=""><p>Thank you. Your Email was sent successfully. <br> <i class="icofont icofont-checked"></i></p> </div>');
$("#loader").hide();
})
}
});
return false;
}
});
});