我有一个使用 JavaScript 验证字段的联系表单,但是当我将表单链接到我的 PHP 时,它不会发送电子邮件。如果有人可以帮助我,将不胜感激。我不太熟悉 PHP 以及它如何与其他语言交互。
这是一个现场演示。
这是代码:
*编辑*
当前的 JAVASCRIPT:
<script type="text/javascript">
reEmail = /^[\w-|+|'|]+(\.[\w-|+|'|]+)*@([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*?\.[a-zA-Z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/
function myFunction()
{
var y;
y = document.getElementById('name');
var x;
x = document.getElementById('email');
var z;
z = document.getElementById('message');
var e;
e = 0;
if(y.value == "Full Name*"){
alert("Full Name Required");
e = e + 1
return false;
}
if(!(reEmail.test(x.value))){
alert("E-Mail Invalid");
e = e + 1
return false;
}
if(z.value == "Your Message*"){
alert("Please write a message.");
e = e + 1;
return false;
}
if(e < 0){
return false;
} else {
alert("Thank you. Your message has been sent!");
return true;
}
}
</script>
*编辑*
当前的 HTML:
<body>
<div class="form" align="center">
<form onSubmit="myFunction();" name="contact" action="sendform.php" method="post" >
<span style="display:block;">
<input name="name" type="text" id="name" class="txt" maxlength="50" value="Full Name*" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
</span>
<br />
<span style="display:block">
<input name="email" type="text" id="email" class="txt" maxlength="20" value="Your E-Mail*" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" >
</span>
<br />
<span style="display:block">
<textarea name="message" type="text" id="message" class="txt" maxlength="500" value="Your Message*" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Your Message*</textarea>
</span>
</form>
<br />
<span id="required">* Indicates Required Fields</span>
<br />
<input type="submit" onclick="myFunction()" class="submit" value="Submit" >
</div>
</body>
PHP(sendform.php):
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@domain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>