1

我正在为一个学校项目开发一个网站。虽然我主要使用 PHP,但我认为我也应该做一些客户端验证。在 Google 中搜索很短的时间就找到了 Validate JQuery 插件。到目前为止,这就是我所拥有的:

<form id="regform" class="well" method="post" action="Registering.php">
        <label>Name: <input type="text" placeholder="Type your name" name="Name" class="required" /></label>
        <label>Surname: <input type="text" placeholder="Type your surname" name="Surname" class="required" /></label>
        <label>Username: <input type="text" placeholder="Type your username" name="Username" class="required" /></label>
        <label>Password: <input type="password" placeholder="Type your password" name="Password" class="required" /></label>
        <label>Repeat Password: <input type="password" placeholder="Confirm Password" name="Password-confirm" class="required" /></label>
        <label>E-Mail: <input type="Email" placeholder="Type your Email" name="Email" class="required email" /></label>
        <label>Birthday: <input type="Date" name="Birthday" /></label>
        <label>Gender:</label>
        <label>Male <input type="radio" name="Gender" value="0" checked> Female <input type="radio" name="Gender" value="1"></label>
        <label>Photo: <input type="url" placeholder="Paste an image link" name="PhotoLink" class="url" /></label>
        <label><input type="submit" class="btn btn-primary" name="Submit" value="Register in YouTune" /></label>
 </form>

这是表格,几乎是标准的注册表格。这里没有什么要补充的。然后我有这个小Javascript:

$(document).ready(function(){
        $("#regform").validate({
            rules: {
                Username: {
                    remote: { url: "/check_username.php", async: false }
                }
            },
            messages: {
                Username: { remote: "Username already on use. Pick a different one." }
            }
        });
});

最新的 JQuery 库的脚本和 Validate Plugin 的 1.10 版本都被添加到文档的头部。

好吧,简而言之,验证工作顺利进行,除了远程规则不起作用。就好像它根本不存在,无论我是否使用已使用的用户名提交都没有错误消息。Javascript 调用的 php 文件具有以下脚本:

<?php error_reporting(E_ALL);
     require_once("../Clases/User.php");
     header('Content-type: application/json');
     $vUser = new User();
     $vResult = $vUser->GetUserByUsername($_REQUEST["Username"]);
     if ($vResult) {
    echo false;
     } else { echo true; }

?>

GetUserByUsername 方法转到数据库并使用给定参数搜索用户,然后如果匹配则返回 true,如果不匹配则返回 false。我已经在没有来自 Javascript 的远程调用的情况下对其进行了测试,它工作正常

那么,有谁知道为什么会这样?

预先感谢您花时间阅读并尝试帮助我解决这个小问题。

编辑:解决。我只需要修复远程呼叫的来源。不敢相信我错过了。无论如何,非常感谢您的帮助。

4

1 回答 1

0

我不太确定,但看看它是否有效;从您的网址中删除“/”。使用“check_username.php”代替“/check_username.php”或绝对网址

于 2012-10-09T14:02:42.157 回答