0

我有一个代码,其中有一个函数应该检查用户名是否可用。

我对这个功能的工作原理感到困惑。

JS:

jQuery(document).ready(function(){
    // binds form submission and fields to the validation engine
    jQuery("#register-form").validationEngine();
});

HTML 输入:

<form action="" id="register-form">
    <input value="" class="validate[required,custom[onlyLetterNumber],maxSize[20],ajax[ajaxNameCallPhp]]] input" type="text" name="username"/>
</form>

这是我的 JS 函数:

(function($){
    $.fn.validationEngineLanguage = function(){};
    $.validationEngineLanguage = {
        newLang: function(){
            $.validationEngineLanguage.allRules = {

                "ajaxNameCallPhp": {
                    // remote json service location
                    "url": "ajaxNameCallPhp.php",
                    // error
                    "alertText": "* This name is already taken",
                    // speaks by itself
                    "alertTextLoad": "* Validating, please wait"
                }
            };
        }
    };

    $.validationEngineLanguage.newLang();
})(jQuery);

这是我的ajaxNameCallPhp.php功能:

include ("config.php");
$username = trim(strtolower($_POST['username']));
$username = mysql_escape_string($username);

$query = "SELECT username FROM user WHERE username = '$username' LIMIT 1";
$result = mysql_query($query);
$num = mysql_num_rows($result);

echo $num;
mysql_close();

所以任何人都可以给我一个解决方案?

我感谢并感谢您的关注。

谢谢你。

4

1 回答 1

1

尝试这个

function UserNameExist(){ 
    $("#msg").html( "* Validating, please wait" );
    $.ajax({
      url: "ajaxNameCallPhp.php",
      type: "POST",
      data: {username : $('#username').val()},
      dataType: "html"
    }).done(function(msg) {
      if(msg > 0){
        $("#msg").html( "* This name is available" );
      }else{
        $("#msg").html( "* This name is already taken" );
      }
    });
   }

其中#msg 是显示按摩的 div 的 id

#userName 是文本框的 id

希望有所帮助。

于 2012-09-13T06:40:01.720 回答