0

该表单似乎已验证并转到确认网址。我遇到的问题是表单没有将表单数据发送到我的电子邮件。我在 JS 方面相当薄弱,希望您的一些专家可以帮助我处理这段代码。网站页面的链接是:http ://www.thenaturalcottage.com/ContactUs.html 。

text/javascript 文件的文件是http://www.thenaturalcottage.com/contact.js 我一直在格式化代码以适应这里,但代码一直抛出错误。

联系信息

姓名

地址

城市, 州, 邮编*

    <p>Telephone<span class="red">*</span></p>
    <p>(<input type="text" name="area" size="3" maxlength="3" onchange="return checkForNumber(this.value);" />
    <input type="text" name="exchange" size="3" maxlength="3" onchange="return checkForNumber(this.value);" />
    <input type="text" name="phone" size="4" maxlength="4" onchange="return checkForNumber(this.value);"  /></p>
    <p>E-mail address<span class="red">*</span><br />
    <input type="text" name="email" size="50" value="Enter your email address" onclick="if(this.value=='Enter your email address') this.value='';" / ></p>
    <p><strong>How do you perfer us to contact you</strong><br />        
    <input type="radio" name="contact" value="Phone" />Phone<br />
    <input type="radio" name="contact" value="email" />Email <br />
    <input type="radio" name="contact" value="mail" />Snail mail
    </p>
    <p>Which one of our services are you interested in recieving information about?</p>
<p>
   <input type="checkbox" name="services" value="classes" /> Classes <br />
   <input type="checkbox" name="services" value="events" /> Events <br />
   <input type="checkbox" name="services" value="customfragarance" /> CustomFragrance    <br />
   <input type="checkbox" name="services" value="suggestions" /> Suggestions<br />
   <input type="checkbox" name="services" value="other" /> Other
   </p>
   <h5>Please enter any comments here:</h5>
   <textarea name="comments" rows="10" cols="40">
   </textarea>

   <p><input type="submit" value="Submit" /></p>
   <p><input type="reset" value="Start Over" /></p>
</form>

这是Javascript

function checkForNumber(fieldValue) {
   //validation for phone number
   var numberCheck = isNaN(fieldValue);
   if(numberCheck == true) {  
       window.alert("You must enter a numeric value!");
       }
}

function confirmSubmit(){
       //confirm submit
       var submitForm = window.confirm("Are you sure you want to submit the form?");
       //check to make sure that none of the text areas are blank
       if (document.forms[0].name.value == "" || document.forms[0].address.value == "" || document.forms[0].city.value == ""
                             || document.forms[0].state.value == "" || document.forms[0].zip.value == ""){
                             window.alert("You must enter your contact information.");
                             return false;
                             }
        //phone validation
        else if (document.forms[0].area.value == "" || document.forms[0].exchange.value == "" || document.forms[0].phone.value == "") {
                             window.alert("You must enter your telephone number.");
                             return false;
                             }
        //email error message
        else if (document.forms[0].email.value == ""){
                             window.alert("You must enter a correct email address");
                             return false;
                             }
       onsubmit="return confirmSubmit();"
}

function confirmReset(){
       var resetForm = window.confirm("Are you sure you want to reset the form?")
       if (resetForm == true)
       return true;
       return false;                     
       }

function submitForm(){
       if (document.forms[0].firstName.value == "" || document.forms[0].lastName.value == ""){
       window.alert("You must enter your first and last name!");
       return false;
       }
       else 
       return true;       
}

function contactMe(){
       for(var i=0; i<document.forms[0].delivery.length; ++i)
       {
       if (document.forms[0].delivery[i].checked == true)
       document.forms[0].delivery[i].checked = false;
       }
}
4

1 回答 1

0

您必须使用 PHP 发送邮件。

更改onsubmit="return confirmSubmit();"confirmSubmitdocument.getElementById("form").submit();并添加 id 以形成 live<form id="form" ...

将此 JS 和 id 添加到您的字段中,以便 JS 可以获取值:

function mail() {
    var req = new Request({
        method: 'get',  
        url: 'mail.php',
        data: {
       'name':document.getElementById("name").value,
       'adress':document.getElementById("adress").value,
       'city':document.getElementById("city").value
    },
    }).send();
}

并制作一个名为的 PHP 文件mail.php

<?php 
//Email Validation
$email = $_GET["email"];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//    echo "Your email is ok.".'<br />';
} else {
echo "Wrong email address format.".'<br />'; die();                     
} 

date_default_timezone_set('UTC');
// sending email via PHP's Mail()
$to      = $email;
$subject = 'Contact mail subject';
$message = "Name: '.($_GET["name"]).'<br />
            Adress '.($_GET["adress"]).'
            ETC...........";
$headers = 'From: cherie@thenaturalcottage.com' . "\r\n" .
'Reply-To: you@yourdomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

强烈建议您对输入字段和邮件库使用验证,而不是简单且易受攻击的 PHP 邮件。

希望这可以帮助。

于 2013-06-07T03:47:22.980 回答