javascript - jQuery Validation 插件远程规则更改 php 函数值

标签 javascript php jquery codeigniter jquery-validate

我有一个“编辑用户表单”,以便在需要时更改用户数据。这些字段之一是 ID 号。除了 Codeigniter 的表单验证库之外,我还使用 jQuery 验证插件。

我的管理员中有这个 php 函数,用于检查为用户输入的 ID 是否已存在于数据库中。如果该 id 与用户存储在数据库中的 id 匹配,那就没问题了。但是当尝试输入另一个用户的 ID 时,会出现错误。如果尝试输入一个未存储在数据库中的 ID,那么应该没问题,并且不会显示任何错误。

我为此构建了一个运行良好的 php 函数。问题是当执行 jquery 函数来调用该 php 函数时。

这是 View :

<div class="span6">
  <div class="control-group">
    <label class="control-label">N&uacute;mero<span class="required">*</span></label>
      <div class="controls">
        <input type="text" name="documentn" id="documentn" class="m-wrap span8" value="{$frontuser->document}" required/>
            <span class="help-block"></span>
      </div>
  </div>
</div>

这是 jquery 函数:

documentn: {
  required: true,
  minlength: 7,
  maxlength: 20,
  remote: {
  url: '/admin/checkDocAndUser',
  type: 'POST',
   },  
 },

这是 PHP 函数:

public function checkDocAndUser(){
    if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
        $n = $_POST['documentn'];
        $id = $_POST['id'];
        $dn = UserManager::getInstance()->checkUserDocument($n,$id);
        if ($dn) {
            //id belongs to the user
            echo "true";
        }else{
            //does the id entered belongs to another user?
            $exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
            if (!$exists) {
                // number entered belongs to another user
                echo "false";
            }
        }
    }
}

因此,当我不使用 js 验证文件时,php 函数可以工作...为什么 jquery 函数不起作用?

我尝试将远程规则 url 更改为:admin/checkDocAndUser、/checkDocAndUser、checkDocAndUser 但不起作用。

编辑和工作

考虑了 @Sparky 所说的内容,现在我正在发送用户 ID

documentn: {
  required: true,
  minlength: 7,
  maxlength: 20,
  remote: {
    url: '/admin/checkDocAndUser',
    type: 'POST',
    data: {
     id: function(){
     var dn = $('#id').val();
     return dn;
    }                                
   } 
  }
},

public function checkDocAndUser(){
    if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
        $dn = UserManager::getInstance()->checkUserDocument($_POST['documentn'],$_POST['id']);
        if ($dn) {
            //id belongs to the user
            echo "true";
        }else{
            //does the id entered belong to another user?
            $exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
            if (!$exists) {
                // number entered belongs to another user
                echo "true";
            }else{
                echo "false";
            }
        }
    }
}

最佳答案

1) 验证您是否在页面上包含了 jQuery 验证插件。它需要包含在 jQuery 库之后。

2) 在您的 PHP 函数中,您似乎正在寻找两个 POST 输入:if ((isset($_POST['documentn'])) && (isset($_POST) ['id'])))。但是,默认情况下,remote 方法仅发送来自您正在验证的字段 documentn 的数据。如果您需要发送另一个字段的值,则需要使用remote...

中的data选项对其进行配置
documentn: {
    required: true,
    // minlength: 7,     // <- can be replaced with rangelength rule
    // maxlength: 20,    // <- can be replaced with rangelength rule
    rangelength: [7,20], // <- can be used in place of two rules
    remote: {            // <- by default, only posts data from 'documentn' field       
        url: '/admin/checkDocAndUser',
        type: 'POST',
        data: {          // <- also post data from another field(s)
            anotherfield: function() {  // <- post name, $_POST['anotherfield']
                return $( "#another_field" ).val();
            }
        }
    }  
},

3)我不确定您的 checkDocAndUser 函数中的逻辑。当它不属于其他用户但存在时会发生什么?在这种情况下你没有回应任何事情。当验证应该通过时,remote 方法需要一个 true 以及一个 falseundefinednull 当它应该失败时。如果它回显 JSON 字符串,则它会失败并且该字符串将成为错误消息。验证您的逻辑是否按照您的预期执行 as per the docs .

参见文档:http://jqueryvalidation.org/remote-method/

关于javascript - jQuery Validation 插件远程规则更改 php 函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23999494/

相关文章:

javascript - 如何简化或干燥此 jquery 代码的语法

php - Codeigniter 测试 - 调用非对象的成员函数

php - 减少和合并数组

javascript - JQuery <tr> strip 奇数/偶数行

javascript - 无法在我的 php 中包含 .js 文件?

javascript - Jquery 用 .on() 悬停

javascript - 正则表达式:查找函数第一个参数使用的属性

javascript - 菜单打开时移动 View 问题?

javascript - 函数之前的 jQuery 选择器

javascript - 通过 angularjs 将 reCaptcha v3 响应发送到 php api