简体   繁体   中英

Form submit and recaptcha

What is wrong with this code ?

 $(function() {
            $('#my_form').submit(function(e) {
                e.preventDefault();
                recaptcha_response_field = $('input#recaptcha_response_field').val();
                recaptcha_challenge_field = $('input#recaptcha_challenge_field').val();

                $.post('controller.php', {recaptcha_response_field : recaptcha_response_field, recaptcha_challenge_field : recaptcha_challenge_field}, function(data) {
                if(data == 0) {
                    alert('no');
                } else {
                    alert('submit')
                    $('#my_form').submit();
                }
            }); 
            })
        });

I use recaptcha in my form. When the user entered correctly the captcha code these lines of code are executed

alert('submit')
$('#my_form').submit();

So fat so good. The problem is that the form is not getting to submit. I think this line of code is executed again

$('#my_form').submit(function(e) {

Any suggestions ?

You can unbind it after all the verfication using

$('#my_form').unbind('submit');

Then submit it regularly using

$('#my_form').submit();

I haven't tested this out yet, but try this. You're right that the .submit function is being triggered again.

$(function() {
    $('#my_form').submit(function(e) {
        'use strict';

        var recaptcha_response_field = $('input#recaptcha_response_field').val(),
            recaptcha_challenge_field = $('input#recaptcha_challenge_field').val(),
            submitForm = true;

        $.post('controller.php', {
                recaptcha_response_field : recaptcha_response_field,
                recaptcha_challenge_field : recaptcha_challenge_field
            }, function(data) {
                if(data == 0) {
                    submitForm = false;
                }
        }); 

        if(!submitForm) {
            e.preventDefault();
        }
    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM