表单始终提交 Jquery 验证引擎
从今天早上开始,我就被 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jquery.validationEngine-fr.js 中的代码似乎没问题。但是,当您进行完整表单提交时,必须以不同的方式处理它。
我使用自定义 url 进行验证,因为我不希望验证采取与表单相同的操作。
成功后,我分离验证引擎,因为它以某种方式执行无限循环
,因为这是一个表单验证,来自服务器的响应必须与简单的 ajax 验证不同,必须是一个数组数组,如下所示:
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
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:
我在寻找另一个问题时偶然发现了这一点。在 jquery.validationEngine.js 中,
我不知道它是否适用于您的问题,或者您是否解决了它,但该评论似乎与您的问题有关。
另外,我会确保您的 AJAX 返回对您的 jquery 有用的东西。
祝你好运。
I stumbled on this while looking for another problem. In the jquery.validationEngine.js
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.
您需要使用属于 ValidationEngine 一部分的 onAjaxFormComplete 函数。请注意,他们的文档中指出:
然后,您可以访问 4 条信息,但您真正感兴趣的只有状态和形式。
因此,附加到表单的初始代码变为:
现在,只有在没有问题的情况下,表单才应提交。
You need to use the onAjaxFormComplete function that is part of the ValidationEngine. Note in their documentation it states:
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:
Now the form should only submit if there are no issues.
您基本上是告诉它无论在此处调用之后都提交它:
您可能需要使用带有回调函数的检查
,然后将其定义为:
You basically are telling it to submit it regardless after the call in here:
You might need to use a check with a call back function
and then define it as: