0

这段代码就像在 facebook 中更改个人资料用户名一样,我对这段代码有疑问:

$(function(){
 var uid = 1;
 var name_available=0;
 var password_ok=0;
$('#name,#password').bind('keyup',function(){
  var name = $('#name').val();
  var password = $('#password').val();

   if(/[^a-zA-Z_\.0-9]+/.test(name)){
      $('#result').html('Invalid Charcters');
      name_available = 0;
   }else{
     $.ajax({
       url: 'check_user.ajax.php',
       type: 'POST',
       data: 'name='+name,
       success: function(data){
         if(data){
              $('#result').html('available');
              name_available = 1;
         }else{
              $('#result').html('not available');
              name_available = 0;
         }
       }
     });
   }
     $.ajax({
       url: 'check_user.ajax.php',
       type: 'POST',
       data: 'uid='+uid+'&password='+password,
       success: function(data){
         if(data){
              $('#result2').html('true');
              password_ok = 1;
         }else{
              $('#result2').html('false');
              password_ok = 0;
         }
       }
     });

     if(name_available === 1 && password_ok === 1){
        $('#save').removeAttr('disabled');
     }else{
        $('#save').attr('disabled','disabled');
     }
});
});

html代码是:

username:<input type="text" name="name" value="" id="name" /><span id="result"></span><br />

password:<input type="password" name="password" value="" id="password" /><span id="result2"></span><br />

<input type="submit" name="save" value="save changes" id="save" disabled /><br />

ajax.php oage 是:

include 'config.php';

if(isset($_POST['name'])){
$name = $_POST['name'];
    if(!empty($name)){
    $sel = mysql_query("SELECT `id` FROM `users` WHERE `name`='$name'");
    $num = mysql_num_rows($sel);
    if($num > 0){
        echo false;
    }else{
        echo true;
    }
}else{
  echo false;
}
}else{
echo false;
}

if(isset($_POST['password']) && isset($_POST['uid'])){
$uid      = $_POST['uid'];
$password = md5($_POST['password']);
$sel      = mysql_query("SELECT `id` FROM `users` WHERE `id`='$uid' AND `password`='$password'");
$num      = mysql_num_rows($sel);
if($num < 1){
    echo false;
}else{
    echo true;
}
}else{
echo false;
}

该代码有效,但并非总是在更改用户名或密码然后更改用户时

请帮助我,这对我很重要。

4

1 回答 1

0

如果我没记错的话,您正在尝试检查用户名是否已被使用并暗示用户输入另一个用户名。

我在我的项目中有一个类似的实现,它检查 ID 是否已被使用并提示用户输入另一个。请看下面的代码:

        var checkIndicatorId = function () {
        var indId = {
            'Id': $('#IdDescriptor').val()
        };

        $('#spnConfirmation img').remove();  **//removes the x or check image** 
        $('#spnConfirmation').append('<img class="icon" src="/CSS/Contents/loader.gif" title="Checking for availability.">');

        $.ajax({   //ajax call for checking data on the server
            url: 'http://localhost:54004/Cd/CheckIndicatorId',
            data: indId,
            type: 'GET',
            contentType: 'application/json;charset=utf-8'

        }).success(function (result) {   **// on success do this**
            if (result == true) {       **if id is available**
                $('#spnConfirmation img').remove();
                $('#spnConfirmation').append('<img class="icon" src="/CSS/Contents/check.png" title="Id is available.">');
            }else {              **//if is is already taken**
                $('#spnConfirmation img').remove();
                $('#spnConfirmation').append('<img class="icon" src="/CSS/Contents/error.png" title="Id is not available. Please enter another one.">');
            }
        }).error(function () {

        });
    };

添加:后端代码:

            var context = new MyContext();
            var result = context.Cd.Count(x => x.IdDescriptor == id);
            if (result < 1)
            {
                return Json(true, JsonRequestBehavior.AllowGet); //this is where the result is thrown back to the front end
            }
            else
            {
                var recommendedId = context.Cd.OrderByDescending(x => x.IdDescriptor + 1).FirstOrDefault();
                return Json(recommendedId, JsonRequestBehavior.AllowGet);//this is where the result is thrown back to the front end
            }

如果您有一些澄清或问题,请发表评论,我很乐意提供帮助。

于 2013-04-25T02:36:13.270 回答