0

だから私はjquery検証でajaxフォームを作成することができたので、php検証で強化する必要があります。コードは次のとおりです。

<!-- Contact form -->
    <div id='contact_form_holder'>
<form action='index.php' method='post' id='contact_form'>

<p>
Your Name:
<div id='name_error' class='error'><img src='images/error.png'> Please enter your Name.</div>
<div><input class="textbox" type='text' name='name' id='name'></div>
</p>

<p>
Your Email:
<div id='email_error' class='error'><img src='images/error.png'> Please enter a valid Email.</div>
<div><input class="textbox" type='text' name='email' id='email'></div>
</p>


<p>
The Subject:
<div id='subject_error' class='error'><img src='images/error.png'> What is the purpose of the contact?</div>
<div><input class="textbox" type='text' name='subject' id='subject'></div>
</p>


<p>
The Message:
<div id='message_error' class='error'><img src='images/error.png'> Forgot why you came here?</div>
<div><textarea class="textbox" name='message' id='message'></textarea></div>
</p>

<div id='mail_success' class='success'><img src='images/success.png'> Your email has been submitted.Thank you.</div>
<div id='mail_fail' class='error'><img src='images/error.png'> There seems to be an internal error.Try later.</div>
<p id='cf_submit_p'>
<input class="button" type='submit' id='send_message' value='Send The Message'>
</p>


</form>  
</div>
    <!-- /Contact form -->

メール送信のphpコード

    $email_to =   'admin@localhost.com'; //the address to which the email will be sent
    $name     =   $_POST['name'];  
    $email    =   $_POST['email'];
    $subject  =   $_POST['subject'];
    $message  =   $_POST['message'];

    /*the $header variable is for the additional headers in the mail function,
     we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
     That way when we want to reply the email gmail(or yahoo or hotmail...) will know 
     who are we replying to. */
    $headers  = "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";

    if(mail($email_to, $subject, $message, $headers)){
        echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..      
    }else{
        echo 'failed';// ... or this one to tell it that it wasn't sent    
    }
?>

そしてjquery:

/// Contact Form ///

    $(document).ready(function(){
        $('#send_message').click(function(e){

            //stop the form from being submitted
            e.preventDefault();

            /* declare the variables, var error is the variable that we use on the end
            to determine if there was an error or not */
            var error = false;
            var name = $('#name').val();
            var email = $('#email').val();
            var subject = $('#subject').val();
            var message = $('#message').val();
            var re = new RegExp(/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/);
            /* in the next section we do the checking by using VARIABLE.length
            where VARIABLE is the variable we are checking (like name, email),
            length is a javascript function to get the number of characters.
            And as you can see if the num of characters is 0 we set the error
            variable to true and show the name_error div with the fadeIn effect. 
            if it's not 0 then we fadeOut the div( that's if the div is shown and
            the error is fixed it fadesOut. 

            The only difference from these checks is the email checking, we have
            email.indexOf('@') which checks if there is @ in the email input field.
            This javascript function will return -1 if no occurence have been found.*/

            if(name.length == 0){
                var error = true;
                $('#name_error').fadeIn(500);
            }else{
                $('#name_error').fadeOut(500);
            }
            if(email.lenght == 0 || !email.match(re)) {
                var error = true;
                $('#email_error').fadeIn(500);
            } else {
                $('#email_error').fadeOut(500);
            }
            if(subject.length == 0){
                var error = true;
                $('#subject_error').fadeIn(500);
            }else{
                $('#subject_error').fadeOut(500);
            }
            if(message.length == 0){
                var error = true;
                $('#message_error').fadeIn(500);
            }else{
                $('#message_error').fadeOut(500);
            }

            //now when the validation is done we check if the error variable is false (no errors)
            if(error == false){
                //disable the submit button to avoid spamming
                //and change the button text to Sending...
                $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

                /* using the jquery's post(ajax) function and a lifesaver
                function serialize() which gets all the data from the form
                we submit it to send_email.php */
                $.post("send_email.php", $("#contact_form").serialize(),function(result){
                    //and after the ajax request ends we check the text returned
                    if(result == 'sent'){
                        //if the mail is sent remove the submit paragraph
                         $('#cf_submit_p').remove();
                        //and show the mail success div with fadeIn
                        $('#mail_success').fadeIn(500);
                    }else{
                        //show the mail failed div
                        $('#mail_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message                        
                        $('#send_message').removeAttr('disabled').attr('value', 'Send The Message');
                        $(':input','#contact_form')
                         .not(':button, :submit, :reset, :hidden')
                         .val('')
                        .removeAttr('checked')
                        .removeAttr('selected');

                    }
                });
            }
        });    
    });

私はphpの知識が非常に乏しく、javascriptが無効になっている場合、このフォームをphpで検証し、jqueryが使用する対応するエラーdiv内にエラーを出力するのを手伝ってくれる人が本当に必要です。助けていただければ幸いです。

4

2 に答える 2

0

index.php で、html フィールドの値を確認します

$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if($_REQUEST['name']!="" && $_REQUEST['email']!="" && $_REQUEST['subject'] !="" &&       $_REQUEST['message']!="")
{
if (preg_match($regex, $_REQUEST['email'])) {
 echo $email . \" is a valid email. We can accept it.\";
 } else { 
 echo $email . \" is an invalid email. Please try again.\";
}

}
else{
echo "Error";
}
于 2012-05-08T09:33:13.153 に答える
0

Redirection http://example.com/form.php?error=1 を介してエラー コードをフォームのページに渡し、$_GET["error"] でエラー コードを確認し、関連付けられているメッセージを表示します。ページ内の必要な場所にエラーがある

于 2012-05-08T09:37:20.320 に答える