如何通过jquery创建两个依赖字段

时间:2013-04-25 02:25:28

标签: jquery ajax

此代码就像更改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;
}

代码可以工作,但不是在更改用户名或密码然后更改用户

的所有时间

请帮帮我,对我来说很重要。

1 个答案:

答案 0 :(得分: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
            }

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