jQuery Validate issue with username checking with database
I got this code from the internet and it work perfectly for the email but with username there is a problem. When ever i type something it display the error 'Username have been taken' even with spam. I just wonder if anyone can fix it up. Thanks.
My Script
- $(document).ready(function () {
- $('.registerForm').validate({
- onkeyup: false,
- rules: {
- username: {
- required: true,
- minlength: 5,
- maxlength: 20,
- remote: {
- url: 'core/check2.php',
- type: "post",
- data:
- {
- username: function() {
- return $( "#username" ).val();
- }
- }
- }
- },
- password: {
- required: true,
- minlength: 5,
- maxlength: 20
- },
- confirmpassword: {
- required: true,
- minlength: 5,
- maxlength: 20,
- equalTo: password
- },
- email: {
- required: true,
- remote: {
- url: 'core/check.php',
- type: "post",
- data: {
- email: function() {
- return $( "#email" ).val();
- }
- }
- }
- },
- code: {
- required: true,
- remote: {
- url: 'core/checkcode.php',
- type: "post",
- data: {
- code: function() {
- return $( "#code" ).val();
- }
- }
- }
- }
- },
- messages: {
- username: {
- required: "Enter a username.",
- minlength: "Username too short",
- maxlength: "Username too long",
- remote: "Username have been taken"
- },
- password: {
- required: "Enter a password.",
- minlength: "Password too short",
- maxlength: "Password too long",
- remote: "Email have been taken"
- },
- confirmpassword: {
- required: "Confirm your password.",
- minlength: "Password too short",
- maxlength: "Password too long",
- equalTo: "Your password does not match"
- },
- email: {
- required: "Enter your email(NOT SPAM EMAIL).",
- remote: "This Email have been taken"
- },
- code: {
- required: "Enter your Register Code",
- remote: "Invalid Code"
- }
- }
-
- });
- });
This is the php for username.
-
- <?php
- include_once("db_connect.php");
- // USERNAME
- if(!empty($_POST['username'])) {
- $username = $mysqli->real_escape_string($_POST['username']);
- $query = "SELECT * FROM users WHERE username = '$username'";
- $results = $mysqli->query($query);
- if($results->num_rows == 0)
- {
- echo "true"; //good to register
- }
- else
- {
- echo "false"; //already registered
- }
- } else {
- echo "false";
- }
- ?>