问题描述:

I am having problem to see the problem with a pre-scripted template.

There are 3 files as follows,

/contact.php

/forms/contact.php

/js/forms.js

So when a visitor fills up the contact.php in root directory it redirects to /forms/contact.php and forms.js checks the form and if there is no problem sends an email.

Here is my /contact.php which includes the form,

<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>

Here is my /forms/contact.php

<?php

$to = '[email protected]';

//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);

?>

and lastly forms.js is here,

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

}

}

So, leaving the form sends Values :S and does not check anything. Shall I try to fix this or try to implement something else? I am not that into jQuery and cannot say if there is something wrong with validation script.

Some advice would be great!

Thank you!

网友答案:

There are errors on .js syntax, this could stop some js code from being called. First of all, fix these errors in js like this:

formid.find("input").each(function ()
{
    if(!jQuery(this).hasClass("button")) jQuery(this).val("");  
})//this is missing a ;

After that you can think properly on post method like Kevin Schmid said..

网友答案:

The validator function check each input with ".requiredfield" class. Sample Code :

formid.find(".requiredfield").each(function () {
相关阅读:
Top