使用PHP不发送电子邮件的JavaScript表单验证

时间:2013-09-10 19:14:47

标签: php javascript html forms

我有一个联系表单,对字段使用JavaScript验证,但是当我将表单链接到我的PHP时,它不会发送电子邮件。如果有人能帮助我,我将不胜感激。我不太熟悉PHP以及它与其他语言的交互方式。

这是live demo

以下是代码:

*的 修改 *

CURRENT 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
}
?>

3 个答案:

答案 0 :(得分: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,有时你不会返回false,当你这样做时,你会在递增e之前返回false ,在这种情况下e实际上不会被更改。你应该总是在这里做同样的事情。

另外,在你的html中:

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

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

答案 1 :(得分:1)

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

HTML:

我认为最好写一下:

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

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

JavaScript:

您已经写了var evar yvar zvar x而没有;

当您检查z.value == 'requiredfield'是否在}之前写了返回false时。

并且e永远不会递增,因为你在增量之前有return false;阻止它递增。

如果您修复了这些错误并尝试了代码,则不会发送页面刷新和电子邮件,然后回复评论,我将很乐意为您提供帮助。

编辑:

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

之前

答案 2 :(得分:0)

我认为这是因为它:

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

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

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

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