2

I am writing a php page with a working form where I want add invisible recaptcha. The form is this:

<form action="#" id="contact-form">
    <div class="input-group name-email input-field">
        <input type="text" name="name" id="name" placeholder="Name" class="form-control">
    </div>
     <!-- RECAPTCHA div-->
    <div class="g-recaptcha" data-sitekey=[key] data-callback="onSubmit" data-size="invisible">
    </div>
    <input type="button" id="form-submit" class="pull-right" value="Send">
 <form>

To Validate the form I use this:

<script type="text/javascript">
        $(function(){
            alert("Just Before Contact Form Validate");
            $('#contact-form').validate({
                rules: {
                        name: {
                        required: true,
                        minlength: 2
                    }
                },
                messages: {
                    name: {
                        required: "What's your name?",
                        minlength: "Name min.2 chars"
                    }
                },
                submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        type:"POST",
                        data: $(form).serialize(),
                        url:[Another PHP page],
          success: {[do this]
          },
          error: {[do that]
          }etc etc

I put the alert for debugging reason; when I load the page I receive it. After the form I included two external js files for validation:

        <!-- Contact form validation -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>

To handle recaptcha, after the form and before the javascript form with the alert I added

<script>
function onSubmit(token) {
    alert("Inside OnSubmit");
    var element=document.getElementById('contact-form');
    element.submit();
}
</script>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<script>
    $(function() {
        $("#form-submit").on('click',function() {
        grecaptcha.execute();
        alert('g-recaptcha executed');
        return false;
        });
    });
</script>

I put these alerts too for debugging reasons. When I load the page I get the first alert; no errors reported in the console part. I press "Snd", even without filling the fields, and I get the "g-recaptcha executed" and "inside on Submit", then the page reloads without validating the inputs. I would expect it to stop and display the errors, but it goes on and now my browser is not anymore in myPage.php but in myPage.php?name=&g-recaptcha-response=[long string]. Still, no error displayed in console. How can I fix it? Can anybody help me please? Thank you.

colecmc
  • 3,133
  • 2
  • 20
  • 33
SCdev
  • 101
  • 2
  • 12
  • We can't help fix your validation rules if we don't know what the validation rules are. Please see the Minimal, Complete, and Verifiable example: https://stackoverflow.com/help/mcve. – colecmc May 22 '18 at 16:02
  • Also, for reCaptcha to prevent form submission, you need some logic that says if recaptcha is valid, do this, else do that. Please see this example: https://stackoverflow.com/questions/29612879/google-recaptcha-how-to-make-required/29613089#29613089 – colecmc May 22 '18 at 16:04
  • Thank you @colecmc for your reply. I edited the post to add my validation; obviously the form is more complex but I stripped to the minimum to focus on the captcha part. Anyway, the form works without the captcha, so I do not think the problem is there. As for the second comment I thought I had to do the check in the php file that receives the form, where I could check the response and see if it was correct using captcha's private key also, or am i missing something? thanks again – SCdev May 23 '18 at 22:28
  • I believe the keys are to get the reCaptcha to work on your site in general. Sorry, I'm not that familiar with the backend. – colecmc May 23 '18 at 23:24
  • Thank you anyway colecmc. If I am not wrong, from what I understood in recaptcha's specification, first you have to get a code then, in the backend, you have to check it by sending a request with your private key to the google servers; you will recevice a json with the result. My problem is that the normal validation is entirely skipped when I add recaptcha. Furthermore the browser reloads the php page and does not go to che form destination and I cannot understand why. – SCdev May 24 '18 at 15:00

0 Answers0