2013-07-05 49 views
0

正如标题所说,我正在尝试使用Ajax检查注册表单中是否存在DB中的用户名。检查用户名可用性ajax

我使用下面的脚本:

$(document).ready(function() { 

    //the min chars for username 
    var min_chars = 3; 

    //result texts 
    var characters_error = 'Minimum amount of chars is 3'; 
    var checking_html = 'Checking...'; 

    //when button is clicked 
    $('#user_id').keyup(function(){ 
     //run the character number check 
     if($('#user_id').val().length < min_chars){ 
      //if it's bellow the minimum show characters_error text ' 
      $('#username_availability_result').html(characters_error); 
     }else{ 
      //else show the cheking_text and run the function to check 
      $('#username_availability_result').html(checking_html); 
      check_availability(); 
     } 
    }); 

}); 

//function to check username availability 
function check_availability(){ 

    //get the username 
    var user_id = $('#user_id').val(); 

    //use ajax to run the check 
    $.post("db.php", { user_id: user_id }, 
     function(result){ 
      //if the result is 1 
      if(result == 1){ 
       //show that the username is available 
       $('#username_availability_result').html(user_id + ' is Available'); 
      }else{ 
       //show that the username is NOT available 
       $('#username_availability_result').html(user_id + ' is not Available'); 
      } 
     }); 

} 

和我db.php中文件看起来像这样:

if (isset($_POST['action']) && $_POST['action'] == "signup") { 
$user_id  = mysql_real_escape_string($_POST["user_id"]); 
$new_password = md5(mysql_real_escape_string($_POST["new_password"])); 
$new_email = mysql_real_escape_string($_POST["new_email"]); 
$checking_existance=mysqli_query($str,"SELECT username FROM users WHERE username='".$user_id."'"); 
$row=mysqli_fetch_assoc($checking_existance); 
print_r($checking_existance); 
if(mysqli_num_rows($checking_existance)>0) 
{ 
    echo 0; 
} 
else{ 
    echo 1; 
} 

}

我遇到的问题是结果总是显示用户名即使是不可用。

+0

MD5是不安全的。你应该使用bcrypt。 – SLaks

+0

散列转义字符串是没有意义的。 – SLaks

+0

'action'没有被传入你的'ajax'调用 – AlexP

回答

0

我发现这个有用的you.You可以检查您的解决方案以下链接Click HereHere

0

db文件正在寻找一些$_POST变量,你还没有通过。

action, new_password and new_email

我猜想,如果你没有console.log(result)你会得到undefined,因此它不是= 1,因此进入else语句。

您需要将AJAX电话更改为:

$.post("db.php", { 
    user_id: user_id, 
    action: action, 
    new_password: new_password, 
    new_email: new_email 
}, ... 
+0

除此之外,OP通过print_r($ checking_existance);打印整个结果,所以结果永远不会是0或1 –

+0

如果我不使用它们来获得结果,为什么我需要传递密码和电子邮件? db.php中的'if'语句不需要'new_password'也不需要'new_email'来执行! – Suvimo

+0

@Suvimo您正在使用它们!而不是在查询中,但是页面正在使用'POST'变量,你不会发布,这意味着一个错误! – AlexP

0

只是做了一个简单的方法(i found that there is no action, new_password and new_email parameters in your javascript)请照顾这一点。

的Javascript

var min_chars = 3; 
var characters_error = 'Minimum amount of chars is 3'; 
var checking_html = 'Checking...'; 
var xhr_request = false; 
var usernames = {}; 

$(function() { 
    $("#user_id").on("keyup", check_availability); 
}); 

function check_availability() { 
    if (xhr_request) { 
    xhr_request.abort(); 
    } 
    var user_id = $.trim($(this).val()); 
    if (!user_id || user_id.length < min_chars) { 
    $("#username_availability_result").html(characters_error); 
    return; 
    } 

    // check user_id exists in runtime cache, to avoid useless requests :\ 
    if (usernames[user_id] !== undefined) { 
    if (usernames[user_id] === true) { 
     //show that the username is available 
     $("#username_availability_result").html(user_id+" is Available"); 
    } 
    else { 
     //show that the username is NOT available 
     $("#username_availability_result").html(user_id+' is not Available'); 
    } 
    return; 
    } 

    //use ajax to run the check 
    xhr_request = $.post("c.php", { 'action' : 'validate', 'user_id' : user_id }); 
    xhr_request.success(function(result) { 
    //if the result is 1 
    if (result == 1) { 
     // add user_id to cache 
     usernames[user_id] = true; 
     //show that the username is available 
     $("#username_availability_result").html(user_id+" is Available"); 
    } 
    else { 
     // add user_id to cache 
     usernames[user_id] = false; 
     //show that the username is NOT available 
     $("#username_availability_result").html(user_id+' is not Available'); 
    } 

    }); 
    xhr_request.fail(function() { 
    $("#username_availability_result").html("connection error, please try again"); 
    }); 
} 

PHP

<?php 
    if (isset($_POST["action"]) && isset($_POST["user_id"])) { 
    $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db"); 
    $userid = $mysqli->real_escape_string($_POST["user_id"]); 

    if ($_POST["action"] === "validate") { 
     $result = $mysqli->query("SELECT username FROM users WHERE username = '{$userid}'"); 

     if ($result && $result->num_rows > 0) { 
     echo "0"; 
     } 
     else { 
     echo "1"; 
     } 
    } 
    elseif ($_POST["action"] == "signup") { 
     // do the process 
    } 
    } 
?> 
+0

谢谢!但是这比我的简单吗?你正在使用几乎相同的方法! – Suvimo

+0

为什么在这个级别需要'new_password'和'new_email'?我们没有使用它们来检索结果! – Suvimo

+0

大声笑,我同意,我的意思是减少无用的多个请求。 – bystwn22