表单始终提交 Jquery 验证引擎

发布于 2024-11-29 07:59:19 字数 1623 浏览 2 评论 0原文

从今天早上开始,我就被 Jquery 验证引擎 和 codeigniter 困住了。我有一个包含姓名、电子邮件、密码的表单...我使用 Jquery 检查所有这些字段,然后在 php 中检查。

当用户输入已使用的电子邮件时,会出现一个工具箱,并显示错误消息,但表单仍已提交...如果我留空电子邮件字段,则不会提交。 所以我猜当它检查数据库时,它之后不会执行良好的操作(它返回 false...)

这是我认为的代码:

    <?php $data_email_signup = array(
        'name'        => 'email_signup',
        'id'          => 'email_signup',
        validate[required,custom[email],ajax[ajaxEmailCall]]'
                        );
            echo form_input($data_email_signup); ?>
...
...

jQuery(document).ready(function(){
    jQuery("#form_signup").validationEngine({
        submitHandler: function(form) {
            form.submit();
        }
    });
});

jquery.validationEngine-fr.js 我有:

"ajaxEmailCall": {
     "url": "/.../ajaxValidateFieldEmail.php",
     "alertTextLoad": "* Please wait",
     "alertText": "* Email already used"
                },

在文件 ajaxValidateFieldEmail.php 中,我有:

<?php
$validateValue=$_POST['fieldValue'];
$validateId=$_POST['fieldId'];
$arrayToJs = array();
$arrayToJs[0] = $validateId;
$resultats=$connexion->query("SELECT ...");
$count = $resultats->rowCount();                
if($count == 0){        
    $arrayToJs[1] = true;
    echo json_encode($arrayToJs);
}
else {
    $arrayToJs[1] = false;
    echo json_encode($arrayToJs);
}
?>

Firebug 返回 FALSE,当我输入已使用的电子邮件时,它会显示 电子邮件已使用,但我不明白为什么该表单无论如何都经过验证。

有人有想法吗?

I'm stuck since this morning with the Jquery Validation Engine and codeigniter. I have a form with name, email, password... and I check all these fiels with Jquery and then in php.

When the user enters an already used email, a toolbox appears with the error message, BUT the form is still submitted... If I leave an empty email field, it is not submitted.
So I guess when it checks in the database it doesn't do the good action afterwards (it returns false...)

This is the code in my view:

    <?php $data_email_signup = array(
        'name'        => 'email_signup',
        'id'          => 'email_signup',
        validate[required,custom[email],ajax[ajaxEmailCall]]'
                        );
            echo form_input($data_email_signup); ?>
...
...

jQuery(document).ready(function(){
    jQuery("#form_signup").validationEngine({
        submitHandler: function(form) {
            form.submit();
        }
    });
});

In jquery.validationEngine-fr.js I have:

"ajaxEmailCall": {
     "url": "/.../ajaxValidateFieldEmail.php",
     "alertTextLoad": "* Please wait",
     "alertText": "* Email already used"
                },

In the file ajaxValidateFieldEmail.php I have:

<?php
$validateValue=$_POST['fieldValue'];
$validateId=$_POST['fieldId'];
$arrayToJs = array();
$arrayToJs[0] = $validateId;
$resultats=$connexion->query("SELECT ...");
$count = $resultats->rowCount();                
if($count == 0){        
    $arrayToJs[1] = true;
    echo json_encode($arrayToJs);
}
else {
    $arrayToJs[1] = false;
    echo json_encode($arrayToJs);
}
?>

Firebug returns FALSE and it displays Email already used when I enter an already used email but I don't understand why the form is validated anyway.

Does anyone has an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场信仰旅途 2024-12-06 07:59:19

jquery.validationEngine-fr.js 中的代码似乎没问题。但是,当您进行完整表单提交时,必须以不同的方式处理它。
我使用自定义 url 进行验证,因为我不希望验证采取与表单相同的操作。
成功后,我分离验证引擎,因为它以某种方式执行无限循环

$("#frmAddUser").validationEngine({ajaxFormValidation: true,
      ajaxFormValidationURL: '/users/validate',
      onAjaxFormComplete: function(status, form, json_response_from_server, options)  {
          if (status === true) {
             //validationengine falling into infinite loop
             form.validationEngine('detach');
             form.submit();
          }
      }
 });

,因为这是一个表单验证,来自服务器的响应必须与简单的 ajax 验证不同,必须是一个数组数组,如下所示:

[[fieldId1, status1, message1], [fieldId2, status2, message2]/*optional*/]

The code in jquery.validationEngine-fr.js seems to be ok. But when you are doing a full form submit, it has to be treated in a different way.
I use a custom url for validation purposes, because I don't want validation take the same action as the form.
On success I detach the validationengine because in someway it was performing an infinite loop

$("#frmAddUser").validationEngine({ajaxFormValidation: true,
      ajaxFormValidationURL: '/users/validate',
      onAjaxFormComplete: function(status, form, json_response_from_server, options)  {
          if (status === true) {
             //validationengine falling into infinite loop
             form.validationEngine('detach');
             form.submit();
          }
      }
 });

Because this is a form validation the response from server has to be different to a simple ajax validation, has to be an array of array like this:

[[fieldId1, status1, message1], [fieldId2, status2, message2]/*optional*/]
疾风者 2024-12-06 07:59:19

我在寻找另一个问题时偶然发现了这一点。在 jquery.validationEngine.js 中,

_onSubmitEvent: function() {
            var form = $(this);
            var options = form.data('jqv');

            // validate each field (- skip field ajax validation, no necessary since we will perform an ajax form validation)

我不知道它是否适用于您的问题,或者您是否解决了它,但该评论似乎与您的问题有关。

另外,我会确保您的 AJAX 返回对您的 jquery 有用的东西。

祝你好运。

I stumbled on this while looking for another problem. In the jquery.validationEngine.js

_onSubmitEvent: function() {
            var form = $(this);
            var options = form.data('jqv');

            // validate each field (- skip field ajax validation, no necessary since we will perform an ajax form validation)

I don't know you if it applies to your problem, or if you solved it, but that comment seems to relate to your problem.

Also, I would make sure your AJAX was returning something useful to your jquery.

Good luck.

姐不稀罕 2024-12-06 07:59:19

您需要使用属于 ValidationEngine 一部分的 onAjaxFormComplete 函数。请注意,他们的文档中指出:

请注意,如果您使用 ajax 表单验证器,实际结果将异步传递到函数 options.onAjaxFormComplete。

然后,您可以访问 4 条信息,但您真正感兴趣的只有状态和形式。

因此,附加到表单的初始代码变为:

    jQuery(document).ready(function(){
        jQuery("#form_signup").validationEngine({
            onAjaxFormComplete: function(status,form) {
                if (status === true) {
                    form.submit();
                }
            }
        });
    });

现在,只有在没有问题的情况下,表单才应提交。

You need to use the onAjaxFormComplete function that is part of the ValidationEngine. Note in their documentation it states:

Note that if you use an ajax form validator, the actual result will be delivered asynchronously to the function options.onAjaxFormComplete.

You then have access to 4 pieces of information, however the only ones you're really interested are status and form.

So your initial code to attach to the form becomes:

    jQuery(document).ready(function(){
        jQuery("#form_signup").validationEngine({
            onAjaxFormComplete: function(status,form) {
                if (status === true) {
                    form.submit();
                }
            }
        });
    });

Now the form should only submit if there are no issues.

花海 2024-12-06 07:59:19

您基本上是告诉它无论在此处调用之后都提交它:

    **submitHandler: function(form) {

           form.submit();
      }**

您可能需要使用带有回调函数的检查

  function ajaxValidationCallback(status, form, json, options)
  {
 if (status === true) form.submit();
  } 

,然后将其定义为:

                         jQuery(document).ready(function($){
                            $("#form_signup").validationEngine({
                                           ajaxFormValidation: true,
                                           ajaxFormValidationMethod: 'post',
                                           onAjaxFormComplete: ajaxValidationCallback 

                                   });
                                          });

You basically are telling it to submit it regardless after the call in here:

    **submitHandler: function(form) {

           form.submit();
      }**

You might need to use a check with a call back function

  function ajaxValidationCallback(status, form, json, options)
  {
 if (status === true) form.submit();
  } 

and then define it as:

                         jQuery(document).ready(function($){
                            $("#form_signup").validationEngine({
                                           ajaxFormValidation: true,
                                           ajaxFormValidationMethod: 'post',
                                           onAjaxFormComplete: ajaxValidationCallback 

                                   });
                                          });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文