0

I am having some problems adding a reCaptcha to my jQuery AJAX form.

I have tried following the documentation, in particular this page, but had no luck.

  • If I use the "Challenge and Non-JavaScript API", adding the code before the send button, I get no output.
  • If I try with the method called "AJAX API" adding a custom div inside the form, I don't get anything anyway. Basically I am not able to show and then validate it.

This is the code that I have so far.

My form:

  <div id="contactForm"><img src="img/contact-form.png" width="250" height="365" alt="contact" /></div>
  Name: <span class="contactErrorFloat" id="err-name">Need the name</span>
  <input name="name" id="name" type="text" placeholder="Name.." />

  Email: <span class="contactErrorFloat" id="err-email">Need email</span><span class="contactErrorFloat" id="err-emailvld">Email not valid.</span>
  <input name="email" id="email" type="text" placeholder="Email.." />

  Message:
  <textarea name="message" id="message" rows="10" placeholder="Message.."></textarea>

  <button id="send">Send</button>

  <div class="contactError" id="err-form">Error during validation</div>
  <div class="contactError" id="err-timedout">Timeout</div>
  <div class="contactError" id="err-state"></div>

  <div id="ajaxsuccess">Email sent!</div>
</form>

My Script:

jQuery(document).ready(function ($) {
    $('#send').click(function(){
        $('.error').fadeOut('slow'); // Resetta i messaggi di errore, nascondendoli

        var error = false;

        var name = $('input#name').val();
        if (name == "" || name == " ") {
            $('#err-name').fadeIn('slow');
            error = true;
        }

        var email_compare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/;
        var email = $('input#email').val();
        if (email == "" || email == " ") {
            $('#err-email').fadeIn('slow');
            error = true;
        } else if (!email_compare.test(email)) {
            $('#err-emailvld').fadeIn('slow');
            error = true;
        }

        if (error == true) {
            $('#err-form').slideDown('slow');
            return false;
        }

        var data_string = $('#ajax-form').serialize();

        $.ajax({
            type: "POST",
            url: $('#ajax-form').attr('action'),
            data: data_string,
            timeout: 6000,
            error: function(request, error) {
                if (error == "timeout") {
                    $('#err-timedout').slideDown('slow');
                } else {
                    $('#err-state').slideDown('slow');
                    $('#err-state').html('C\'è stato un errore: ' + error + '');
                }
            },
            success: function () {
                $('ajax-form').slideUp('slow');
                $('#ajaxsuccess').slideDown('slow');
            }
        });

        return false;
    });
});

There is also a PHP file with the php function to send the email but I don't think it's much important actually. I would really LOVE if someone could give me any help to implement this. Thanks a lot!

4

1 回答 1

1

首先你必须登录recaptcha。您可以在这里执行此操作: recaptcha.net

然后你就可以拿到你的钥匙了。然后将密钥嵌入到示例代码中。这里

<script type="text/javascript"
 src="http://www.google.com/recaptcha/api/challenge?k=**your_public_key**">

和这里

 <iframe src="http://www.google.com/recaptcha/api/noscript?k=**your_public_key**"
     height="300" width="500" frameborder="0"></iframe>
于 2013-01-03T11:33:44.193 回答