我想使用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); ?>
此代码始终显示一个错误,即使没有使用用户名也是如此。
我正在使用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; ?>
谢谢大家在这里的帮助:)