我在互联网上找到了一个用于用户名验证的免费脚本,这是它的 javascript 部分:
$(document).ready(function () {
$("#username").blur(function () {
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the username exists or not from ajax
$.post("availability.php", {
user_name: $(this).val()
}, function (data) {
if (data == 'no') //if username not avaiable
{
$("#msgbox").fadeTo(200, 0.1, function () //start fading the messagebox
{
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900, 1);
});
} else {
$("#msgbox").fadeTo(200, 0.1, function () //start fading the messagebox
{
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900, 1);
});
}
});
});
});
你看到这个引用了页面“availability.php”,代码如下:
<?
$existing_users=array('roshan','mike','jason');
$user_name=$_POST['user_name'];
if (in_array($user_name, $existing_users))
{
echo "no";
}
else
{
//user name is available
echo "yes";
}
?>
最后,在页面上,这是用户在其中输入数据的输入标记:
<input name="user_name" type="text" id="username" value="" maxlength="15" />
<span id="msgbox" style="display:none"></span>
我有这个脚本使用最新版本的 jquery (1.4.4) 这是工作示例的链接:Link
当您在我的网站中输入“mike”时,它会提示该用户名可供使用。在我上面提供的示例链接中,用户名被采用,就像它应该的那样。
我能想到的唯一可能的问题是我的主机不提供对ajax的支持? 想法?
谢谢!
最佳答案
检查 firebug、chrome 等中的响应...无论您使用什么,我的猜测是 “no”
并不是唯一在响应中回显,您可以在 $.post()
中成功处理程序的顶部执行一个简单的 alert(data)
来查看其中可能还包含哪些内容。确保仅在响应中回显您想要的内容。
关于php - 用户名可用性检查未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4424886/