引导formvalidation.io远程验证器MySQL PHP

bootstrap formvalidation.io remote validator MySQL PHP

本文关键字:验证 MySQL PHP formvalidation io 程验证 引导      更新时间:2023-09-26

我有一些问题得到一个模态形式验证用户名从MySQL数据库使用PHP。

这是我的PHP脚本来验证用户名,当我单独运行它的工作,但当它不会从远程验证器调用。

PHP代码:

checkUsername.php

<?php
    $isAvailable = true;
    //get the username  and password
    $uname = trim($_POST['username']);
    $umail = trim($_POST['email']);
    //connect to database   
    require_once '/php-includes/dbconfig.inc.php';
    $stmt = $DB_con->prepare("SELECT username, email FROM member WHERE username=:uname OR email=:umail");
    $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);

    if($row['username']==$uname) {
        $isAvailable = false; 
    }
    // Finally, return a JSON
    echo json_encode(array('valid' => $isAvailable));
?>

,这是formValidation。IO脚本,我正在使用从http://formvalidation.io/examples/adding-warning-validation-state/

$(document).ready(function() {
    $('#registerForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                userName: {
                    validators: {
                        notEmpty: {
                            message: 'The user name is required'
                        },
                        remote: {
                            url: 'checkUsername.php'
                        }
                    }
                }
            }
        })
        // This event will be triggered when the field passes given validator
        .on('success.validator.fv', function(e, data) {
            // data.field     --> The field name
            // data.element   --> The field element
            // data.result    --> The result returned by the validator
            // data.validator --> The validator name
            if (data.field === 'userName'
                && data.validator === 'remote'
                && (data.result.available === false || data.result.available === 'false'))
            {
                // The userName field passes the remote validator
                data.element                    // Get the field element
                    .closest('.form-group')     // Get the field parent
                    // Add has-warning class
                    .removeClass('has-success')
                    .addClass('has-warning')
                    // Show message
                    .find('small[data-fv-validator="remote"][data-fv-for="userName"]')
                        .show();
            }
        })
        // This event will be triggered when the field doesn't pass given validator
        .on('err.validator.fv', function(e, data) {
            // We need to remove has-warning class
            // when the field doesn't pass any validator
            if (data.field === 'userName') {
                data.element
                    .closest('.form-group')
                    .removeClass('has-warning');
            }
        });
});

让它像这样工作

                remote: {
                    url: '/includes/checkUser.inc.php',
                    message: {
                        en_US:  "This usenrame is already taken, please choose another one",
                        fr_FR:  "Ce nom d'utilisateur est déjà pris, 
                    },
                    data: {
                        type: 'username'
                    },
                    type: 'POST',
                    delay: 1000
                }