使用PHP的Parsley(2.8.1)唯一用户名验证 [英] Parsley (2.8.1) unique username validation with php

查看:84
本文介绍了使用PHP的Parsley(2.8.1)唯一用户名验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了Parsley Js网站的所有文档.但是我对如何根据AJAX响应设置自定义验证的理解还不够.

I read all the documentation of the Parsley Js web site. But i not get enough understand how to set custom validation depend on AJAX response.

我需要验证用户名,如果是,请检查数据库中是否已存在该用户名,并在单击提交"按钮时显示错误.

I need to validate a username and check if it is already exist in the database if it is yes and show the error when hit the submit button.

我在这里四处张望,并试图找到与此有关的任何问题,我无法接受,只有我知道应如何放置Ajax代码才能验证该表格.我需要知道PHP文件的外观.那就是为什么我发布这个问题

I look around here and try to find any question regarding to this i could not fine that only i got how ajax code should place to validate that forms. I need to know how the PHP file look likes. That why i post that question

我正在使用欧芹js新版本.这是我的代码,就像

I am using parsley js new version. Here is my code look like

<form class="forms-sample" id="form">
        <div class="form-group">
            <label for="exampleInputUsername1">Username</label>
            <input type="text" class="form-control" id="exampleInputUsername1" 
             name="username" placeholder="Username" required data-parsley-username>
        </div>
        <button type="submit" class="btn btn-success mr-2">Submit</button>
        <button class="btn btn-light">Cancel</button>
    </form>

我正在使用CSS的Bootstrap 4.我的Parsley js脚本是这样的

and i am using Bootstrap 4 for CSS. My Parsley js script like this

        <script>
     window.ParsleyValidator
            .addValidator('username', function (value, requirement) {
                var response = false;

                $.ajax({
                    url: "username.php",
                    data: {username: value},
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function(data) {
                        response = true;
                    },
                    error: function() {
                        response = false;
                    }
                });
                return response;
            }, 32)
            .addMessage('en', 'username', 'The username already exists.');

    $("#form").parsley({
    errorClass: 'is-invalid',
    successClass: 'is-valid',
    errorsWrapper: '<span class="invalid-feedback"></span>',
    errorTemplate: '<span></span>',
    trigger: 'change'

});
</script>

我无法想象我如何创建PHP文件来编写查询以及我想从该PHP中回显什么.

I cannot imaging how i create PHP file to write query and what i want to echo from that PHP.

能否请您在此处告诉我任何需要更改的内容,您能否指导我构建该PHP文件来完成此任务.我需要知道这个JS是否可以工作.谢谢.

Could you please tell me anything need to be change here and can you guide me to build that PHP file to complete this task. And i need to know this JS will work or not. Thank you.

我添加了一个php文件.就是这样

After i added a php file. that is something like this

<?php 
        include("include/connection.php");
    ?>
    <?php
        if(isset($_POST['username'])){
            $username = $_POST['username'];
            $query = "SELECT * FROM user WHERE userName = '%$username'";
            $q = mysqli_query($conn, $query);
            if(mysqli_num_rows($q) > 0) { 
                echo json_encode("error");
            }else { 
                echo json_encode("success");
            } 
        }
    ?>

但是,每次出现用户名已存在"时,我都会收到默认错误消息.有人知道这是什么错误吗?

but i got default error message for every time that was "The username already exists." Can anybody know what is the error in here?

推荐答案

在username.php中,您将对传递的用户名运行基本的选择查询,然后根据是否找到结果返回数据

in username.php you're going to run a basic select query on the username you pass, then return the data based on whether it finds a result or not.

以下内容应该非常接近.

The following should be pretty close.

PHP

if ($user = $mysqli->query("SELECT * FROM `user_table_name` WHERE `username_field_name` = $_POST['username']"))) {
    print(json_encode(['status'] => 'user_exists']))

} else {
    print(json_encode(['status'] => 'user_available']))
}

AJAX

$.ajax({
    url: "username.php",
    data: {username: value},
    dataType: 'json',
    type: 'post',
    async: false,
    success: function(data) {
        if( data.status === 'user_available ) {
        //continue your code
        } else {
        //alert user that the name is unavailable
        }
    }

这篇关于使用PHP的Parsley(2.8.1)唯一用户名验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆