我在使用预先编写脚本的模板时遇到问题。 有3个文件如下,
/contact.php
/forms/contact.php
/js/forms.js
因此,当访问者填写根目录中的contact.php时,它会重定向到/forms/contact.php,而forms.js会检查表单,如果没有问题则发送电子邮件。
这是我的/contact.php,其中包含表单
<form id="contactform" method="post" action="forms/contact.php">
<div class="divide10"></div>
<input id="name" name="name" value="Your Name" type="text" class="prepared-input">
<div class="divide15"></div>
<input id="email" name="email" value="Your Email" type="text" class="prepared-input">
<div class="divide15"></div>
<textarea id="contactmessage" name="message" rows="3" class="prepared-input">Your Message</textarea>
<div class="divide15"></div>
<input type="submit" id="From_Comment_Go" value="Send Message " class="btn maincolor small">
<span class="errormessage hiddenatstart">Error! Please correct marked fields.</span>
<span class="successmessage hiddenatstart">Message send successfully!</span>
<span class="sendingmessage hiddenatstart">Sending...</span>
</form>
这是我的/forms/contact.php
<?php
$to = 'example@mail.com';
//Language Options
$contact_labelmailhead = 'Contact Form Email';
$contact_labelmailsubject = 'Contact Form Email from';
$contact_labelname = 'Name';
$contact_labelemail = 'Email';
$contact_labelmessage = 'Message';
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = str_replace(chr(10), "<br>", $_POST['message']);
$body = "<html><head><title>$contact_labelmailhead</title></head><body><br>";
$body .= "$contact_labelname: <b>" . $name . "</b><br>";
$body .= "$contact_labelemail <b>" . $email . "</b><br>";
$body .= "$contact_labelmessage:<br><hr><br><b>" . $message . "</b><br>";
$body .= "<br></body></html>";
$subject = $contact_labelmailsubject.' ' . $name;
$header = "From: $email\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=utf-8\n";
mail($to, $subject, $body, $header);
?>
最后forms.js就在这里,
jQuery(document).ready(function() {
/* Contact Form */
if(jQuery('#contactform').length != 0){
addForm('#contactform');
}
/* Quick Contact */
if(jQuery('#quickcontact').length != 0){
addForm('#quickcontact');
}
/* Blog Comments */
if(jQuery('#replyform').length != 0){
addForm('#replyform');
}
});
function addForm(formtype) {
var formid = jQuery(formtype);
var emailsend = false;
formid.find("input[type=submit]").click(sendemail);
function validator() {
var emailcheck = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var othercheck = /.{4}/;
var noerror = true;
formid.find(".requiredfield").each(function () {
var fieldname = jQuery(this).attr('name');
var value = jQuery(this).val();
if(value == "Name *" || value == "Email *" || value == "Message *"){
value = "";
}
if(fieldname == "email"){
if (!emailcheck.test(value)) {
jQuery(this).addClass("formerror");
noerror = false;
} else {
jQuery(this).removeClass("formerror");
}
}else{
if (!othercheck.test(value)) {
jQuery(this).addClass("formerror");
noerror = false;
} else {
jQuery(this).removeClass("formerror");
}
}
})
if(!noerror){
formid.find(".errormessage").fadeIn();
}
return noerror;
}
function resetform() {
formid.find("input").each(function () {
if(!jQuery(this).hasClass("button")) jQuery(this).val("");
})
formid.find("textarea").val("");
emailsend = false;
}
function sendemail() {
formid.find(".successmessage").hide();
var phpfile = "";
if(formtype=="#contactform"){
phpfile = "forms/contact.php";
}else if(formtype.lastIndexOf("c_")){
phpfile = "forms/quickcontact.php";
}else{
phpfile = "";
}
if (validator()) {
if(!emailsend){
emailsend = true;
formid.find(".errormessage").hide();
formid.find(".sendingmessage").show();
jQuery.post(phpfile, formid.serialize(), function() {
formid.find(".sendingmessage").hide();
formid.find(".successmessage").fadeIn();
if(!formtype.lastIndexOf("c_"))resetform();
});
}
}
return false
}
}
因此,离开表单会发送值:S并且不会检查任何内容。我应该尝试解决这个问题还是尝试实施其他方法?我不是那个jQuery,不能说验证脚本是否有问题。
一些建议会很棒! 谢谢!
答案 0 :(得分:0)
.js语法有错误,这可能会阻止某些js代码被调用。首先,在这样的js中修复这些错误:
formid.find("input").each(function ()
{
if(!jQuery(this).hasClass("button")) jQuery(this).val("");
})//this is missing a ;
之后,你可以正确地思考像Kevin Schmid这样的帖子方法。
答案 1 :(得分:-1)
验证器函数使用".requiredfield"
类检查每个输入。
示例代码:
formid.find(".requiredfield").each(function () {