尝试:
什么有效:
什么不
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js" type="text/javascript"></script>
<script language="javascript" src="validate.js"></script>
<meta name="generator" content="" />
<style type="text/css"></style>
</head>
<body>
<form action="/" method="post" id="register-form">
<div>
<label for="username">Username<font color="red">*</font>:</label>
<input type="text" name="username" size="20" id="username" />
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
</body>
</html>
JQUERY验证
$().ready(function() {
// validate signup form on keyup and submit
$("#register-form").validate({
rules: {
username: {
required: true,
remote: "check-username.php"
},
},
messages: {
username:{
remote: "This username is already taken! Try another."
},
},
});
});
检查-USERNAME.php
<?php
$searchVal = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$data", $username, $password);
$sql = "SELECT * FROM users WHERE USERNAME = " . "'" . $searchVal . "'";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result>0){
return true;
}else {
return false;
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
答案 0 :(得分:7)
为了验证服务器端的值,您应该添加新的验证方法。
$().ready(function() {
$.validator.addMethod("checkUserName",
function(value, element) {
var result = false;
$.ajax({
type:"POST",
async: false,
url: "check-username.php", // script to validate in server side
data: {username: value},
success: function(data) {
result = (data == true) ? true : false;
}
});
// return true if username is exist in database
return result;
},
"This username is already taken! Try another."
);
// validate signup form on keyup and submit
$("#register-form").validate({
rules: {
"username": {
required: true,
checkUserName: true
}
}
});
});
如上所述,方法checkUserName将验证到服务器端并提供错误消息“此用户名已被占用!如果用户名不存在,请尝试另一个。”。
有关jQuery验证及其方法的更多信息和示例:click here。
希望这有帮助。
答案 1 :(得分:1)
没有检查你的其余代码,但试试这个:
<?php
$searchVal = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$data", $username, $password);
$sql = "SELECT * FROM users WHERE USERNAME = " . "'" . $searchVal . "'";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result>0) {
$valid = "true";
} else {
$valid = "false";
}
$dbh = null;
echo $value;
}
catch(PDOException $e) {
echo $e->getMessage();
}
&GT;
答案 2 :(得分:0)
这是使用here的其他插件的解决方案。 要使用Ajax调用检查名称可用性,请执行以下操作:
下载附件jquery.validationEngine.js
和您的语言环境后如下:
<script src="js/jquery.validationEngine-en.js" type="text/javascript" charset="utf-"></script>
<script src="js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
请注意,en
区域设置也是必须包含的。
打开并修改您的语言文件,您必须指出php
文件的位置,该文件将检查数据库的名称可用性或使用默认文件名。因此,如果您使用英语,请在jquery.validationEngine-en.js
中找到以下行。
"ajaxUserCallPhp": {
"url": "phpajax/ajaxValidateFieldUser.php",
如果您愿意,可以在此处更改文件名:
"ajaxUserCallPhp": {
"url": "my-file_which_will_check_db.php",
您的my-file_which_will_check_db.php
应该是这样的:
<?
/* RECEIVE VALUE */
$validateValue=$_REQUEST['fieldValue'];
$validateId=$_REQUEST['fieldId'];
$validateError= "This username is already taken";
$validateSuccess= "This username is available";
/* RETURN VALUE */
$arrayToJs = array();
$arrayToJs[0] = $validateId;
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); //Connect with your credentials
$stmt = $dbh->query("SELECT * From `users` WHERE `USERNAME` = '{$validateValue}'");
$dbh = null; // Disconnect
sleep(1); /*added because check is very fast and the label 'checking...' is not visible*/
if ($result = $stmt->fetch()) {
$arrayToJs[1] = false; // found user with that name
echo json_encode($arrayToJs);
}
else {
$arrayToJs[1] = true;
echo json_encode($arrayToJs); // that user name is free
}
?>
最后,您的HTML标记应如下所示:
<input type="text" name="user_name" id="user_name" class="validate[required],custom[onlyLetterNumber],maxSize[20],ajax[ajaxUserCallPhp] text-input" />
请注意此处的其他class
属性。
并包含以下脚本:
<script>
$(document).ready(function(){
$("#register_form").validationEngine();
});
</script>