gpt4 book ai didi

javascript - bootstrap formvalidation.io 远程验证器 MySQL PHP

转载 作者:行者123 更新时间:2023-11-29 22:10:11 24 4
gpt4 key购买 nike

我在使用 PHP 从 MySQL 数据库获取模态表单验证用户名时遇到一些问题。

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

PHP 代码:

检查用户名.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
}

关于javascript - bootstrap formvalidation.io 远程验证器 MySQL PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31754879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com