I want to validate if username exists in database using jQuery.validate so here's what I have so far:
我想使用jQuery.validate来验证数据库中是否存在用户名,所以这是我到目前为止所拥有的:
jQuery:
$("#signupForm").validate({
rules: {
username: {
required: true,
minlength: 3,
remote: "check-username.php"
}
},
messages: {
username:{
remote: "This username is already taken! Try another."
}
}
});
check-username.php:
<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$name = mysql_real_escape_string($_POST['username']);
$check_for_username = mysql_query("SELECT username FROM mmh_user_info WHERE username='$name'");
if (mysql_num_rows($check_for_username) > 0) {
$output = true;
} else {
$output = false;
}
echo json_encode($output);
?>
This code always shows an error that the username is taken even if it's not.
此代码始终显示用户名被取消的错误,即使不是。
I'm using jQuery Mobile 1.9.1
我正在使用jQuery Mobile 1.9.1
Thanks in advance.
提前致谢。
2 个解决方案
#1
6
I've managed to get this to work by changing the PHP technique I was using, here's my PHP:
我已经设法通过改变我使用的PHP技术来实现这一点,这是我的PHP:
<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$request = $_REQUEST['username'];
$query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'");
$result = mysql_num_rows($query);
if ($result == 0){
$valid = 'true';}
else{
$valid = 'false';
}
echo $valid;
?>
Thanks everyone here for your help :)
在此感谢大家的帮助:)
#2
2
I have two resources for to look at.
我有两个资源可供查看。
Official example from the validate plugin: http://jquery.bassistance.de/validate/demo/milk/
来自validate插件的官方示例:http://jquery.bassistance.de/validate/demo/milk/
jQuery forum solution: http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up
jQuery论坛解决方案:http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up
The possible solution is that the response does not need to be json encoded as far as I can tell. Since json needs key value pairs, suppling just the value won't work. So try to just echo it out as 'true' or 'false' strings.
可能的解决方案是,就我所知,响应不需要进行json编码。由于json需要键值对,因此仅提供值将不起作用。因此,尝试将其作为“true”或“false”字符串回显。
Second, the validate uses GET for the form submission method, not POST.
其次,验证使用GET作为表单提交方法,而不是POST。
NOTE: JUST FOUND POSSIBLE SOLUTION QUESTION jQuery Remote validation
注意:刚刚找到可能的解决方案问题jQuery远程验证
#1
6
I've managed to get this to work by changing the PHP technique I was using, here's my PHP:
我已经设法通过改变我使用的PHP技术来实现这一点,这是我的PHP:
<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$request = $_REQUEST['username'];
$query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'");
$result = mysql_num_rows($query);
if ($result == 0){
$valid = 'true';}
else{
$valid = 'false';
}
echo $valid;
?>
Thanks everyone here for your help :)
在此感谢大家的帮助:)
#2
2
I have two resources for to look at.
我有两个资源可供查看。
Official example from the validate plugin: http://jquery.bassistance.de/validate/demo/milk/
来自validate插件的官方示例:http://jquery.bassistance.de/validate/demo/milk/
jQuery forum solution: http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up
jQuery论坛解决方案:http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up
The possible solution is that the response does not need to be json encoded as far as I can tell. Since json needs key value pairs, suppling just the value won't work. So try to just echo it out as 'true' or 'false' strings.
可能的解决方案是,就我所知,响应不需要进行json编码。由于json需要键值对,因此仅提供值将不起作用。因此,尝试将其作为“true”或“false”字符串回显。
Second, the validate uses GET for the form submission method, not POST.
其次,验证使用GET作为表单提交方法,而不是POST。
NOTE: JUST FOUND POSSIBLE SOLUTION QUESTION jQuery Remote validation
注意:刚刚找到可能的解决方案问题jQuery远程验证