简体   繁体   English

JQuery.Validate()-远程规则

[英]JQuery.Validate() - remote rule

I'm developing a Web site for a school project. 我正在为一个学校项目开发一个网站。 Though I mainly work with PHP, I thoght I should as well do some client-side validation. 尽管我主要使用PHP,但我还是应该做一些客户端验证。 It took a very short search in Google to find the Validate JQuery plugin. 在Google中进行了很短的搜索,以找到Validate JQuery插件。 And so far this is what I've got: 到目前为止,这是我所拥有的:

<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>

This is the form, pretty much a standard registration one. 这是表格,几乎是标准注册表格。 Nothing to add here. 没什么可添加的。 And then I have this small Javascript: 然后我有这个小的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." }
            }
        });
});

Both scripts of the lastest JQuery library and the version 1.10 of the Validate Plugin were added on the head of the document. 最新的JQuery库脚本和版本1.10的Validate Plugin均添加在文档的开头。

Well, to make it short, the validation stuff runs smooth, except that the remote rule is not working. 好了,简而言之,除了远程规则不起作用之外,验证工作运行顺利。 It is as if it wasn't there at all, no error message whether I submit with an already used Username or not. 好像根本就没有,无论我是否使用已经使用过的用户名提交,都不会出现错误消息。 The php file that the Javascript is calling has the following script: 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; }

?> ?>

The GetUserByUsername method goes to a DataBase and searches for a User with the given parameter, then returns true if there was a match up, false if there wasn't. GetUserByUsername方法转到数据库并搜索具有给定参数的User,然后如果匹配成功,则返回true,否则返回false。 I've tested it without the remote call from the Javascript and it works fine 我已经对它进行了测试,而没有从Javascript进行远程调用,它工作正常

So, does anyone know why is this happening? 那么,有谁知道为什么会这样?

Thank you beforehand for taking the time read and trying to help me with this small issue. 预先感谢您抽出宝贵的时间阅读并尝试帮助我解决这个小问题。

EDIT: Solved. 编辑:解决。 I just had to fix the source for the remote call. 我只需要修复远程呼叫的来源。 Can't believe I missed that. 不敢相信我错过了。 Anyway, thank you very much for the help. 无论如何,非常感谢您的帮助。

I am not very sure but see if it works; 我不太确定,但是看看是否可行。 remove "/" from your url. 从您的网址中删除“ /”。 use "check_username.php" in place of "/check_username.php" or absolute url 使用“ check_username.php”代替“ /check_username.php”或绝对网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM