jQuery验证远程方法的用法以检查用户名是否已存在
问题描述:
我想使用jQuery.validate验证数据库中是否存在用户名,所以这是我到目前为止的内容:
I want to validate if username exists in database using jQuery.validate so here's what I have so far:
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:
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.
我正在使用jQuery Mobile 1.9.1
I'm using jQuery Mobile 1.9.1
谢谢.
答
我设法通过更改我使用的PHP技术使它起作用,这是我的PHP:
I've managed to get this to work by changing the PHP technique I was using, here's my 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;
?>
感谢这里的所有人:)