简体   繁体   English

使用jQuery问题验证和提交表单

[英]validating and submitting form using jquery issue

I'm trying to submit form using j-query, before submitting it should do some validation and in that I've validation for capcha using ajax. 我正在尝试使用j-query提交表单,在提交表单之前应该进行一些验证,并且我已经使用ajax验证了capcha。 Everything works fine without ajax validation. 无需ajax验证,一切正常。

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

jquery code 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;

                }

              }
            });
    }
});`

while submitting form it shows alert "enter" but form not getting submitted. 提交表单时,它显示警报“输入”,但表单未提交。 Thanks in advance. 提前致谢。

You probably need to change your variable to a JSON seening how you are retreiving from a php. 您可能需要将变量更改为JSON,以查看如何从php检索。 if thats not the solution try 如果那不是解决方案,请尝试

if (data){

}else{

}

FYI this is not jquery validation. 仅供参考,这不是jquery验证。 Jquery validation is a library set up in order to make sure the proper fields are set up correctly before submitting. jQuery验证是一个库,用于确保在提交之前正确设置了正确的字段。 What you have done is tested if the captcha is correct after submitting. 提交后,验证码(Captcha)是否正确将测试您所做的工作。

try something like this 尝试这样的事情

        $(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;

                            }

                      }
                  });
                }
            });`

        })

Try making ajax synchronous 尝试使Ajax同步

 $.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;

            }

          }
        });

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 . 我认为问题是,每次您再次从commit调用时,ajax请求都返回false以外的值时,我想每次它都调用Submit函数时,但无法成功提交。 because it calls submit function recursively. 因为它以递归方式调用Submit函数。

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');   
                }

              }
            });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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