php – jQuery验证远程方法用法以检查用户名是否已存在
作者:互联网
我想使用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."
}
}
});
入住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);
?>
此代码始终显示用户名被取消的错误,即使不是.
我正在使用jQuery Mobile 1.9.1
提前致谢.
解决方法:
我已经设法通过改变我使用的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;
?>
在此感谢大家的帮助:)
标签:username,php,jquery,mysql,jquery-validate 来源: https://codeday.me/bug/20190911/1803396.html