在 jQuery Validate 的远程方法中从远程 PHP 调用特定函数

Call specific function from remote PHP in jQuery Validate's remote method

我在注册页面上使用 jQuery Validate 插件进行字段验证。

目前我的验证 JS 中有这段代码,用于检查输入的用户名是否已经存在:

"remote": {
    url: "../assets/php/checkUsername.php",
    type: "post",
    data: {
        username: function() {
            return $('#register-form :input[name="username"]').val();
        }
    }
}

checkUsername.php 基本上 returns true 或 false 基于输入的用户名是否存在。这一切都很好,但我想优化它。

现在我有 core.php 文件,其中已经包含名为 account_exists($username) 的函数。此功能基本上 returns true 或 false 也基于给定用户名的存在。

我的问题是,我怎样才能完全摆脱 checkUsername.php 而使用 core.php 文件中的 account_exists 函数?

core.php 显然包含更多内容,所以我不确定如何告诉我的验证脚本从 core.php.

调用特定函数

您可以传递另一个参数,这样 core.php 知道您想要做什么

"remote": {
            url: "../assets/php/core.php",
            type: "post",               
            data: {
                action: 'check_account',
                username: function() {
                    return $('#register-form :input[name="username"]').val();
                }
            }
        }

当然你还必须修改 core.php 以便它执行所需的代码 if action=='check_account':

if ($_POST['action']=='check_account')
{
...do something...
}