该表单似乎已验证并转到确认网址。我遇到问题的地方是表单不会将表单数据发送到我的电子邮件。我在JS方面很弱,希望你的一些专家可以帮我解决这个问题。网站页面的链接是:http://www.thenaturalcottage.com/ContactUs.html。
text / javascript文件的文件是http://www.thenaturalcottage.com/contact.js 我一直在格式化代码以适应这里,但代码不断抛出错误。
名称
地址
城市,州,Zip *
<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;
}
}
答案 0 :(得分:0)
您必须使用PHP发送邮件。
使用onsubmit="return confirmSubmit();"
更改confirmSubmit上的document.getElementById("form").submit();
并添加ID以形成直播<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();
}
使用以下命令生成名为mail.php
的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);
?>
强烈建议您使用验证输入字段和邮件lybrary而不是简单易受攻击的PHP邮件。
希望这有帮助。