首页 > 开发 > PHP > 正文

用户注册检测用户名是否存在ajax+php代码

2024-05-04 21:58:26
字体:大 中 小
来源:转载
供稿:网友

在用户注册检测用户名是否存在我们要提供告诉用户你要注册的用户名是否可用,那么我们就得利用ajax技术来实例,下面是一款ajax php当用户输入完用户名时提示用户是否可用用的代码:

  1. <?php 
  2. $title = isset($_get['title'])?$_get['title']:''; 
  3. if( $title ) 
  4. { 
  5.  $sql ='select id from filecontent where title=''.$title.'''; 
  6.  $q = mysql_query( $sql ) or die( mysql_error()); 
  7.  if( mysql_num_rows( $q )  ) 
  8.  { 
  9.   echo 1; 
  10.  } 
  11.  else 
  12.  { 
  13.   echo 0; 
  14.  } 
  15. } 
  16. else 
  17. { 
  18.  echo 0;//开源代码Vevb.com 
  19. } 
  20. ?> 
  21.  
  22. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  23. <html xmlns="http://www.111cn.net/1999/xhtml"> 
  24. <head> 
  25. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  26. <title> 用户注册检测用户名是否存在ajax + php代码</title> 
  27. <script> 
  28. //  用户注册检测用户名是否存在ajax + php代码 
  29.  
  30.  
  31. function createxmlhttprequest(){//创建xmlhttprequest对象 
  32.  if(window.activexobject){//ie 
  33.   try { 
  34.    return new activexobject("microsoft.xmlhttp"); 
  35.   } catch(e){ 
  36.    return; 
  37.   } 
  38.  }else if(window.xmlhttprequest){//mozilla,firefox 
  39.   try { 
  40.    return new xmlhttprequest(); 
  41.   } catch(e){ 
  42.    return; 
  43.   } 
  44.  } 
  45. } 
  46.  
  47. function getrenews(value){//主调函数 
  48.  var xmlhttp=createxmlhttprequest(); 
  49.  var url = "t.php?action=check&title="+value+"&mt="+math.random(300000); 
  50.  if (value==""){   
  51.   return false ; 
  52.  } 
  53.  if (xmlhttp){ 
  54.   callback = getreadystatehandler(xmlhttp); 
  55.   xmlhttp.onreadystatechange = callback; 
  56.   xmlhttp.open("get", url,true); 
  57.   xmlhttp.send(null); 
  58.  } 
  59. } 
  60. //返回0代表用户名可用,否则提示己被注册。 
  61.  
  62. function getreadystatehandler(xmlhttp){//服务器返回后处理函数 
  63.  return function (){ 
  64.   if(xmlhttp.readystate == 4){ 
  65.    if(xmlhttp.status == 200){ 
  66.         
  67.       
  68.      if (xmlhttp.responsetext==1){ 
  69.        document.getelementbyid("checkid").innerhtml="<font color='red'>对不起,你输入的用户名己被注册!</font>";      
  70.      }else{ 
  71.       document.getelementbyid("checkid").innerhtml="可以注册";      
  72.      }       
  73.    } 
  74.   } 
  75.  } 
  76. } 
  77. </script> 
  78. </head> 
  79.  
  80. <body> 
  81. 给input框增加onblur事件,当用户输入完用户名就检测用户名,并给出提示。 
  82. 输入用户名<input name="title" type="text" id="title" size="40" onblur="getrenews(this.value);"><span id="checkid"></span> 
  83. </body> 
  84. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表