1

AJAXを使用してPHPの連絡フォームにreCAPTCHAを追加しようとしています。ユーザーにreCAPTCHAを表示して、ユーザーに入力してもらいたい。正しくない場合は、ページを離れずに表示する必要があります。CAPTCHAが正しい場合にのみ、CAPTCHAにアクセスしcontacts-go.phpてコードを実行し、電子メールを送信する必要があります。この例からやってみました。

これは私のcontact.phpページにあるものです:

<?php   
    session_start();
?>

<html>
    <head>
        <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
        <SCRIPT type="text/javascript">
            function validateCaptcha()
            {
                challengeField = $("input#recaptcha_challenge_field").val();
                responseField = $("input#recaptcha_response_field").val();
                //alert(challengeField);
                //alert(responseField);
                //return false;
                var html = $.ajax({
                type: "POST",
                url: "ajax.recaptcha.php",
                data: "recaptcha_challenge_field=" + challengeField + "&amp;recaptcha_response_field=" + responseField,
                async: false
                }).responseText;

                if(html == "success")
                {
                    $("#captchaStatus").html(" ");
                    // Uncomment the following line in your application
                    return true;
                }
                else
                {
                    $("#captchaStatus").html("Your captcha is incorrect. Please try again");
                    Recaptcha.reload();
                    return false;
                }
            }

        </SCRIPT>

        <script type="text/javascript">

</script>

    </head>


<body>
    <?php
        require_once("ajax.recaptcha.php");
    ?>

    <div id="login">
        <form method="post" onSubmit="return validateCaptcha()" action="contacts-go.php">

                <label>Message</label>
                <br />
                <textarea type="text" id="name" name="message" size="20"></textarea>

            <p><?php echo recaptcha_get_html($publickey);?></p>

            <p style="color: red;" id="captchaStatus">&nbsp;</p>

            <input type=submit name="submit" value="Submit">
        </form>
    </div>

</body>
</html>

これはajax.recaptcha.phpファイルです

<?php

    //A. Load the Recaptcha Libary
    require_once("recaptchalib.php");
    require_once("recaptha-keys.php");  // my reCAPTCHA Keys

    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if ($resp->is_valid) {
        ?>success<?
        $_SESSION['captcha'] = 1;
    }
    else 
    {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
           "(reCAPTCHA said: " . $resp->error . ")");
    }
?>

私が得ているのは、次の空白のページだけです。

The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

4

1 に答える 1

2
于 2013-01-23T17:57:05.357 に答える