0

事前にスクリプト化されたテンプレートの問題を確認するのに問題があります。以下の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
    }

}

したがって、フォームを離れると Values :S が送信され、何もチェックされません。これを修正しようとしますか、それとも何か他のものを実装しようとしますか? 私はjQueryにそれほど興味がなく、検証スクリプトに問題があるかどうかはわかりません。

いくつかのアドバイスは素晴らしいでしょう!ありがとうございました!

4

2 に答える 2

-1

バリデータ関数は、各入力を".requiredfield"クラスでチェックします。サンプルコード:

formid.find(".requiredfield").each(function () {
于 2013-09-27T03:42:40.823 に答える