php - jQuery.validator.addMethod check username -


i new php , jquery, have googled day couldn't solve problem. want check if username taken or not, got "username taken" no matter enter. here code:

$(document).ready(function(){     $('#registration-form').validate({     rules: {                  username: {             required: true,             minlength: 3,             maxlength: 25,             letters: true,             checkusername: true,         },               password: {             required: true,             minlength: 6,             maxlength: 12         },     confirmpassword: {             required: true,             minlength: 6,             maxlength: 12,             equalto: "#password"         },               email: {             required: true,             email: true         },         country:{             required: true,      }             },          highlight: function(element) {             $(element).closest('.form-group').addclass('error');             $(element).removeclass("valid");         },         success: function(element) {             element             .addclass('valid')             .closest('.form-group').removeclass('error');         }   });    $(".close, #close").click(function() {         $("label.error").hide();         $(".error").removeclass("error");         $("input.valid").removeclass("valid");     });      $.validator.addmethod("letters", function(value, element) {     return this.optional(element) || value == value.match(/^[a-za-z_]+$/);     },"letters , underscore only.");      $.validator.addmethod("checkusername", function(value, element){         var response='';         $.ajax({             type: "post",             url: 'checkname.php',             data:"username="+value,             async:false,             success:function(data){             response = data;             }                 });             if(response == 0)             {                 return true;             }             else             {             return false;             }      }, "username taken");  });  

here checkname.php

<?php include 'connect.php';   if (isset($_post['username'])) {                                                                  $username = mysql_real_escape_string($_post['username']);                                     $check_for_username = mysql_query("select * user username='$username'");  if (mysql_num_rows($check_for_username)>0) {     echo "false";                                                                            } else {     echo "true";                                                                           } }   ?> 

please me, frustrated .....

you should using the remote method, has been tested , proven exact purpose. there no reason reinvent that's been done.

rules: {                 username: {         required: true,         minlength: 3,         maxlength: 25,         letters: true,         remote: {  // value of 'username' field sent default             type: 'post',             url: 'checkname.php'         }     }     .... 

your checkname.php

include 'connect.php';  if (isset($_post['username'])) {                                                                    $username = mysql_real_escape_string($_post['username']);      $check_for_username = mysql_query("select * user username='$username'");       if (mysql_num_rows($check_for_username) > 0)     {         echo json_encode("username taken");            // try this:         // echo json_encode($username . " taken");                                                                             }      else      {         echo "true";                                                                               } } 

note: mysql_ has been deprecated in php , should replaced mysqli or pdo_mysql. see: php.net/manual/en/function.mysql-query.php

also see: jquery validate remote method usage check if username exists


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -