1

我正在尝试使用 j-query 提交表单,在提交之前应该进行一些验证,并且我已经使用 ajax 验证了验证码。没有 ajax 验证一切正常。

<form id="RegistrationForm" name="RegistrationForm" method="post" action="save.php" >

jQuery代码

$('#RegistrationForm').submit(function(){
//some validation goes here
if($('#captcha').val() != '')
    { 
        var captcha = $('#captcha').val();
        $.ajax({
            url:'validate.php',
            type:'POST',
            data:{captcha:captcha},
            success: function(data){
                if(data == 'false')
                {
                    alert('Wrong Captcha is typed!');
                    return false;
                }
                else
                {
                                alert('enter');
                document.getElementById("RegistrationForm").submit();   
                                return true;

                }

              }
            });
    }
});`

提交表单时,它显示警报“输入”但表单未提交。提前致谢。

4

4 回答 4

0

I think the problem is when the ajax request returns other value than false , every time you are again calling from submit , i think every time it calls to submit function , but could not submit successfully . because it calls submit function recursively.

try

$('#RegistrationForm').submit(function(){
//some validation goes here
if($('#captcha').val() != '')
    { 
        var captcha = $('#captcha').val();
        $.ajax({
            url:'validate.php',
            type:'POST',
            data:{captcha:captcha},
            success: function(data){
                if(data == 'false')
                {
                    alert('Wrong Captcha is typed!');
                    return false;
                }
                else
                {
                     alert('enter');   
                }

              }
            });
    }
});
于 2013-11-14T06:50:58.193 回答
0

Try making ajax synchronous

 $.ajax({
        url:'validate.php',
        type:'POST',
        async: false,
        data:{captcha:captcha},
        success: function(data){
            if(data == 'false')
            {
                alert('Wrong Captcha is typed!');
                return false;
            }
            else
            {
                            alert('enter');
            document.getElementById("RegistrationForm").submit();   
                            return true;

            }

          }
        });
于 2013-11-14T06:46:23.873 回答
0

您可能需要将变量更改为 JSON,看看您是如何从 php.ini 中检索的。如果那不是解决方案,请尝试

if (data){

}else{

}

仅供参考,这不是 jquery 验证。Jquery 验证是一个库设置,以确保在提交之前正确设置正确的字段。提交后验证您所做的验证是否正确。

于 2013-11-14T06:44:00.287 回答
0

尝试这样的事情

        $(function(){
            $('#RegistrationForm').submit(function(){
                if($('#captcha').val() != ''){ 
                    var captcha = $('#captcha').val();
                    $.ajax({
                        url:'validate.php',
                        type:'POST',
                        async:false,
                        data:{captcha:captcha},
                        success: function(response){
                            if(response == 'false')
                            {
                                alert('Wrong Captcha is typed!');
                                return false;
                            }
                            else
                            {
                                alert('enter');
                                return true;

                            }

                      }
                  });
                }
            });`

        })
于 2013-11-14T06:44:04.987 回答