这段代码就像在 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;
}
该代码有效,但并非总是在更改用户名或密码然后更改用户时
请帮助我,这对我很重要。