3

我有一个使用 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
}
?>
4

3 回答 3

2

你的问题在这里:

if(z.value == "Your Message*"){
    alert("Please write a message.");
    return false;
    e = e + 1
} else if(z.value == "Required Field"){ 
    z.value = "Required Field"
    e = e + 1
} return false;

由于您在右大括号return false; 之后拥有它,因此您的代码在到达该点时总是停止。

另外,在增加 e 之后是否返回 false 是不一致的;有时你返回false,有时你不返回,当你这样做时,你在增加e之前返回false ,在这种情况下e实际上并没有改变。你应该总是在这里做同样的事情。

此外,在您的 html 中:

<input type="submit" onclick="myFunction()" class="submit" value="Submit" >

onclick应该是return myFunction(),否则表单提交将始终发生,无论您的 javascript 函数的结果如何。

于 2013-09-10T19:23:16.217 回答
1

问题不在您的 PHP 中,而在您的 HTML 和 Javascript 中,因为页面没有重新加载:

HTML:

我认为最好写:

<form onSubmit="MyFunction();" name="contact" action="sendform.php" method="post" >

因为当你这样使用它时,它减少了写入MyFunction()而不是在提交按钮return MyFunction()的侦听器中的错误onClick

JavaScript:

你写了var e, var y, var z,var x没有;

当您检查是否z.value == 'requiredfield'}.

并且e永远不会增加,因为return false;在增加之前你有阻止它增加的东西。

如果您修复了这些错误并尝试了代码,页面刷新并且电子邮件未发送,请在评论中回复,我将很乐意为您提供帮助。

编辑:

现在我知道这些修复后的问题提交按钮不是表单的一部分,因为您关闭表单然后添加提交,所以只需将提交按钮移动到之前</form>

于 2013-09-10T20:48:58.413 回答
0

我认为这是因为它:

if(e > 0){
    return false;   
} else {
    alert("Thank you. Your message has been sent!");
    return true;
}
}

每次验证通过时,你都会在 e 上加 1,它总是大于 0,我不知道它是否是你的错误,但我认为它应该像

if(e != 5){
        return false;   
    } else {
        alert("Thank you. Your message has been sent!");
        return true;
    }
    }

为什么是 5?因为你将 1 加到 e 5 次,所以最后它应该保持 5。

于 2013-09-10T19:20:04.373 回答