当前位置:首页 > PHP教程 > php应用 > 列表

php ajax注册验证用户名是否存在代码

发布:smiling 来源: PHP粉丝网  添加日期:2013-12-11 13:41:50 浏览: 评论:0 

这是注册程序是一款当用户输入完用户名是,就会自动去数据库中查询用户要注册的用户名是否己经被注册了,如果是返回提示否则提示可以注册。

conn.php文件

  1. <html> 
  2. <head> 
  3. <meta http-equiv="content-type" content="text/html; charset=utf-8"
  4. <script > 
  5. var xmlhttp 
  6. function showhint(str) 
  7. if (str.length==0) 
  8.   {  
  9.   document.getelementbyid("txthint").innerhtml="" 
  10.   return 
  11.   } 
  12. xmlhttp=getxmlhttpobject() 
  13. if (xmlhttp==null) 
  14.   { 
  15.   alert ("browser does not support http request"
  16.   return 
  17.   }  
  18. xmlhttp.onreadystatechange=statechanged 
  19. var geturl="conn.php?q="+str 
  20. //sid是增加一个随机数 防止页面启用缓存技术· 
  21. geturl=geturl+"&sid="+math.random() 
  22. geturl=encodeuri(geturl); 
  23. geturl=encodeuri(geturl);  
  24. xmlhttp.open("get",geturl,true) 
  25. xmlhttp.send(null) 
  26. }  
  27. function statechanged()  
  28. {  
  29. if (xmlhttp.readystate==4 || xmlhttp.readystate=="complete"
  30.  {  
  31.  document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext  
  32.  }  
  33. function getxmlhttpobject() 
  34. var xmlhttp=null; 
  35. try 
  36.  { 
  37.  // firefox, opera 8.0+, safari 
  38.  xmlhttp=new xmlhttprequest(); 
  39.  } 
  40. catch (e) 
  41.  { 
  42.  // internet explorer 
  43.  try 
  44.   { 
  45.   xmlhttp=new activexobject("msxml2.xmlhttp"); 
  46.   } 
  47.  catch (e) 
  48.   { 
  49.   xmlhttp=new activexobject("microsoft.xmlhttp"); 
  50.   } 
  51.  } 
  52. return xmlhttp; 
  53. </script>  
  54. </head> 
  55. <body bgcolor="#999999"
  56. <center> 
  57. <form>  
  58. <table> 
  59.  <tr> 
  60.   <td>用户名:</td> 
  61.   <td><input type="text" id="txt1" onkeyup="showhint(this.value)"></td> 
  62.  </tr> 
  63.  <tr align="center"
  64.   <td colspan="2"><span id="txthint"></span></td> 
  65.  </tr> 
  66. </table> 
  67. </form> 
  68. </center> 
  69. </body> 
  70. </html> 
  71.  
  72. <?php 
  73. $q=$_get["q"]; 
  74. $q = urldecode($q); 
  75. if (strlen($q) > 0) 
  76.   $conn = @mysql_connect("localhost","root","1010"or die ("mysql连接错误"); 
  77.   mysql_select_db("xin",$conn); 
  78.   mysql_query("set names 'utf8'"); 
  79.    
  80.   $sql = "select username from message where username = '$q'"
  81.   $query = mysql_query($sql); 
  82.   @$row = mysql_fetch_array($query); 
  83.    
  84.   if(!emptyempty($row['username'])) 
  85.   { 
  86.    $response = "<font color=red>已经被注册!</font>"
  87.   }else 
  88.   { 
  89.    $response = "<font color=blue>恭喜!可以注册!</font>"
  90.   } 
  91.    
  92.   echo $response
  93. ?> 

数据库

  1. drop database if exists `xin`; 
  2. create database `xin` /*!40100 default character set utf8 */; 
  3. use `xin`; 
  4.  
  5. create table `message` ( 
  6.   `id` int(11) not null auto_increment, 
  7.   `username` varchar(20) default null
  8.   primary key  (`id`) 
  9. ) engine=innodb auto_increment=2 default charset=utf8; 

Tags: ajax 注册 验证

分享到: