首页 > 开发 > PHP > 正文

ajax+php验证用户名重复代码实例

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

本教程是一款利用了ajax php在用户输入完用户名后,就会发送请求给php程序,然后查询数据,判断用户要注册的用户名是不是己经注册或存在重复了,及时的返回提示信息,以免用户填写了一大填表单后,突然提供用户名不能注册己被注册了,这样体验就不好了,本教程就是专门解决这个问题了,能快速的告诉你要注册的用户名是否可以注册.

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.Vevb.com/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  5. <title>ajax+php验证用户名重复代码实例</title> 
  6. <script language="javascript"> 
  7. function createxmlhttprequest(){//创建xmlhttprequest对象 
  8.  if(window.activexobject){//ie 
  9.   try { 
  10.    return new activexobject("microsoft.xmlhttp"); 
  11.   } catch(e){ 
  12.    return; 
  13.   } 
  14.  }else if(window.xmlhttprequest){//mozilla,firefox 
  15.   try { 
  16.    return new xmlhttprequest(); 
  17.   } catch(e){ 
  18.    return; 
  19.   } 
  20.  } 
  21. } 
  22. function getrenews(value){//主调函数 
  23.  var xmlhttp=createxmlhttprequest(); 
  24.  var url = "13.php?action=check&title="+value+"&mt="+math.random(300000); 
  25.  if (value==""){   
  26.   return false ; 
  27.  } 
  28.  if (xmlhttp){ 
  29.   callback = getreadystatehandler(xmlhttp); 
  30.   xmlhttp.onreadystatechange = callback; 
  31.   xmlhttp.open("get", url,true); 
  32.   xmlhttp.send(null); 
  33.  } 
  34. } 
  35. function getreadystatehandler(xmlhttp){//服务器返回后处理函数 
  36.  return function (){ 
  37.   if(xmlhttp.readystate == 4){ 
  38.    if(xmlhttp.status == 200){ 
  39.        alert(xmlhttp.responsetext); 
  40.      if (xmlhttp.responsetext==1){ 
  41.        document.getelementbyid("checkid").innerhtml="<font color='red'>对不起,用户名己存在!</font>";      
  42.      }else{ 
  43.       document.getelementbyid("checkid").innerhtml="可以注册";      
  44.      }       
  45.    } 
  46.   } 
  47.  } 
  48. } 
  49. </script> 
  50. </head> 
  51. <body> 
  52. <table width="75%" border="0"> 
  53.   <tr> 
  54.     <td width="12%">输入用户名</td> 
  55.     <td width="36%">     
  56.       <input type="text" name="username" id="username" onblur="getrenews(this.value);" /> 
  57.     </td> 
  58.     <td width="52%" id="checkid">&nbsp;</td> 
  59.   </tr> 
  60. </table> 
  61. </body> 
  62. </html> 

把下面代码保存忝13.php,代码如下:

  1. <?php 
  2. checkusername(); 
  3. function checkusername() 
  4. { 
  5.  $title = trim($_get['title']); 
  6.  if( emptyempty( $title ) ) 
  7.  { 
  8.   return false; 
  9.  } 
  10.  else 
  11.  { 
  12.   mysql_connect('localhost','root','root'); 
  13.   mysql_select_db('test'); 
  14.   mysql_query("set names 'gb2312'"); 
  15.   $sql = "select * from cn_user where username ='$title'"; 
  16.    
  17.   $row = mysql_query($sql); 
  18.    
  19.   if( mysql_num_rows( $row ) ) 
  20.   { 
  21.    echo 1; 
  22.   } 
  23.   else 
  24.   { 
  25.    return null; 
  26.   } 
  27.  } 
  28. }
  29. ?>

创建数据库SQL代码如下:

  1. create table `test`.`cn_user` (  
  2. `id` int not null auto_increment ,  
  3. `username` varchar( 20 ) not null ,  
  4. `times` date null ,  
  5. primary key ( `id` )   
  6. ) engine = myisam  
  7. //插入数据  
  8. insert into `test`.`cn_user` (  
  9. `id` ,  
  10. `username` ,  
  11. `times`   
  12. )  
  13. values (  
  14. null , 'jimmy', null   
  15. ), (  
  16. null , 'www.Vevb.com', null   
  17. );

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