检查用户名是否存在 - 跨浏览器问题!建议?

时间:2013-05-17 17:37:23

标签: php javascript html mysql ajax

我有一个注册表单,它会动态检查数据库中的现有用户名和电子邮件地址。但是,它似乎不适用于FireFox或IE。虽然它在Safari和Chrome上很棒。这是代码:

<input type="text" name="username" id="username" placeholder="Username" class="registerinvitel" onKeyUp="resettooltip();" onFocusOut="check_register_exist('username');" autocomplete="off" />

JavaScript:

function check_register_exist(meth){
    var cueajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        cueajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            cueajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                cueajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    cueajaxRequest.onreadystatechange = function(){
        if(cueajaxRequest.readyState == 4){
var cuerez = cueajaxRequest.responseText;

if ( cuerez == 'exist' ) { infotooltip(meth, cuerez); 

document.getElementById('chk'+meth).value = "exist";

} else { document.getElementById('chk'+meth).value = "ok"; }


        }
    }


    var curchk = document.getElementById(meth).value;
    var queryString = "meth="+meth+"&u=" + curchk;
    cuenocache = Math.random();
    cueajaxRequest.open("GET", "/ajax/check.php?" + queryString + "&nocache=" + cuenocache, true);
    cueajaxRequest.send(null); 


}

我有什么想法可以改变它以使其有效吗? AJAX check.php代码如下(但我不认为这是问题):

<?

include  "../conf/config.php";

if(isset($_GET['meth'])) { $meth = $_GET['meth']; } else { $meth = ''; }
if(isset($_GET['u'])) { $u = $_GET['u']; } else { $u = ''; }


if ( $meth == 'username' ) {


$query_chkuser = mysql_query("SELECT * FROM members WHERE Username = '$u'");
$query_nr_res = mysql_num_rows($query_chkuser);

if ( $query_nr_res == '1' ) { echo "exist"; } else { echo "ok"; }


} 

elseif ( $meth == 'email' ) {


$query_chkuser = mysql_query("SELECT * FROM members WHERE Email = '$u'");
$query_nr_res = mysql_num_rows($query_chkuser);

if ( $query_nr_res == '1' ) { echo "exist"; } else { echo "ok"; }


} 

else {}

1 个答案:

答案 0 :(得分:7)

尝试使用onBlur代替onFocusOut

编辑:或者,甚至更好,onChange - 毕竟,如果用户没有更改文本,则无需重新检查。