用户注册时,不提交表单的情况下,检查用户名是否存在

来源:互联网 发布:域名注册后费用 编辑:程序博客网 时间:2024/06/05 16:31

经常在某些论坛上注册的时候,有一个可以让你检查用户名是否可用的按钮
这个比以前的时候只有提交了页面后才能知道用户名是否可用有了很大的进步
给人很体贴的感觉,今天我就想实现这个功能
baidu了一下,很快发现了第一种方案:xmlhttp

csdn上的一位朋友说可以用xmlhttp,经过先刷新提交的方法来实现
在xmlhttp.cn上找到关于xmlhttp和介绍
“现在的绝对多数浏览器都增加了对XmlHttp的支持,IE中使用ActiveXObject方式创建XmlHttp对象,
其他浏览器如:Firefox、Opera等通过window.XMLHttpRequest来创建xmlhttp对象。”
这就是说firefox对xmlhttp是不支持的,为了验证这个说法,我打开了firefox
可以当页面转到register.shtml的时候,网页的源代码出现在眼前,而不是原来的输入框
这个是怎么回事难道是firefox不支持shtml,此时我更加的糊涂了,先不管这个,先把主要矛盾给解决了
继续回到maxthon中,进行测试.......

经过一番努力,这个问题可以实现了,可是新的问题出现了
server端在经过验证后,client端通过responseText可以得到server端的文件
最后是通过alert来通知用户,该用户名是可用还是不能用的
问题就出在弹出的消息框中,我在server端输出的一些汉字在alert框中全变成了乱码,再仔细研读xmlHttp
其上对responseText的解释这样写到:“变量,此属性只读,将响应信息作为字符串返回。
XMLHTTP尝试将响应信息解码为Unicode字符串,XMLHTTP默认将响应数据的编码定为UTF-8,
如果服务器返回的数据带BOM(byte-order mark),XMLHTTP可以解码任何UCS-2 (big or little endian)
或者UCS-4 数据。注意,如果服务器返回的是xml文档,此属性并不处理xml文档中的编码声明。
你需要使用responseXML来处理。”
哎,可是我对什么bom,ucs-2,ucs-4什么的都不懂,这个问题一时半会儿也解决不了
下面还是接着研究其它的用户名检查方法吧,这个难啃的骨头以后会专门来解决的:)

下面是xmlHttp方法的程序:
client端

<html>
<head>
 <title>注册新用户</title>
<script language="JavaScript">
function AddDataPost(sUserName)
{
    var oBao = new ActiveXObject("Microsoft.XMLHTTP");
    sUserName = escape(sUserName);
 var userInfo = "username="+sUserName;
    oBao.open("POST","userlist.php",false);
    oBao.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    oBao.send(userInfo);
    alert(unescape(oBao.responseText))
}
</script>
</head>
<body>
 <h2>注册新用户</h2>
 <form action="register.php" method="post">
  <div>
   <p>用户名:<input type="text" name="username" maxlength="10">
   <input type="button" onclick="AddDataPost(document.all.username.value)" value="检查用户名是否存在"><br></p>
   <p>密    码:<input type="password" name="password" maxlength="10"></p>
   <p>确认密码:<input type="password" name="passconfirm" maxlength="10"></p>
   <p>电子邮件:<input type="text" name="email"  maxlength="50"></p>
   <p>出生日期:<input type="text" name="birth"  maxlength="10"></p>
   <p><input type="hidden" name="doreg"></p>
   <p><input type="submit" value="注册" name="submit">
   <input type="reset" value="重置" name="reset"></p>
  </div>
 </form>
</body>
</html>

server端

<?php
  /*
  Description:检测用户名是否存在
  CreateDate:2006-4-9
  Creater:alvar
  */  
  $db_name="golfweb";
  $table_name="user";
  $bl_user=false;

  $conn=@mysql_connect("localhost","root","");
  $db=@mysql_select_db($db_name,$conn) or die(mysql_error());
  @$sql="select * from user where username='".$_POST[username]."' order by uid";
  $result=@mysql_query($sql,$conn) or die(mysql_error());
  while ( $row = mysql_fetch_array($result) )
  {
   //$var[$row['sql_name']] = $row['sql_value'];
   $uid=$row['uid'];
   $username=$row['username'];
   $password=$row['password'];
   $email=$row['email'];
   $birthday=$row['birthday'];
   $regtime=$row['regtime'];
   $lastlogtime=$row['lastlogtime'];
  @$display_block.="<ul><li>用户ID:$uid<li>用户名:$username<li>用户密码:$password<li>电子邮件:$email<li>出生日期:$birthday<li>注册时间:$regtime<li>最后一次登录时间:$lastlogtime</ul><br />";
  $bl_user=true;
  }

  if($bl_user==true){
   echo "该用户名已经有人使用!";
  }else if($bl_user==false){
   echo "该用户名您可以使用!";
  }


?>

ps:另外可以用session的方法来检查用户名,但是这种方法必须要求你的提交页面是用php来做
如果想用html,或shtml就不可能了
另外我看到有人提议用隐藏frame来做,个人觉得这种方法有点复杂
不知道还有没有其它的方法,如果哪位朋友知道可以给些提示呵

 

 

原创粉丝点击