好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php+ajax注册验证实例(完整代码)

ajax注册是现在我看到网站中所有网站几乎都会使用到的一个功能,下面我就来给各位整理一个关于ajax注册验证例子,希望此例子能帮助到各位哦.

html代码如下:

< script   src = "ajax.js" > </ script >    < form >    < table >   < tr >      < td > 用户名: </ td >      < td > < input   type = "text"   id = "txt1"   onKeyUp = "showHint(this.value)" > </ td >     </ tr >     < tr   align = "center" >      < td   colspan = "2" > < span   id = "txtHint" > </ span > </ td >     </ tr >   </ table >   </ form >  

js文件代码如下:

var  xmlHttp  function  showHint(str)  {  if  (str.length==0)    {     document.getElementById( "txtHint" ).innerHTML= ""      return     }  xmlHttp=GetXmlHttpObject()  if  (xmlHttp== null )    {    alert ( "Browser does not support HTTP Request" )     return     }   xmlHttp.onreadystatechange=stateChanged  var  geturl= "conn.php?q=" +str  //sid是增加一个随机数 防止页面启用缓存技术·   geturl=geturl+ "&sid=" +Math.random()  geturl=encodeURI(geturl);  geturl=encodeURI(geturl);   xmlHttp.open( "GET" ,geturl, true )  xmlHttp.send( null )  }   function  stateChanged()   {   if  (xmlHttp.readyState==4 || xmlHttp.readyState== "complete" )   {    document.getElementById( "txtHint" ).innerHTML=xmlHttp.responseText    }   }  function  GetXmlHttpObject()  {  var  xmlHttp= null ;  try    {    // Firefox, Opera 8.0+, Safari    xmlHttp= new  XMLHttpRequest();   }  catch  (e)   {    // Internet Explorer     try     {    xmlHttp= new  ActiveXObject( "Msxml2.XMLHTTP" );    }    catch  (e)    {    xmlHttp= new  ActiveXObject( "Microsoft.XMLHTTP" );    }   }  return  xmlHttp;  } 

php操作代码如下:

<?php  $q = $_GET [ "q" ];  $q  = urldecode( $q );  if  ( strlen ( $q ) > 0)  {     $conn  = @mysql_connect( "localhost" , "root" , "1010" )  or   die  ( "MySql连接错误" );    mysql_select_db( "xin" , $conn );    mysql_query( "set names 'utf8'" );         $sql  =  "SELECT username FROM message WHERE username = '$q'" ;     $query  = mysql_query( $sql );    @ $row  = mysql_fetch_array( $query );         if (! empty empty ( $row [ 'username' ]))    { //开源软件:phpfensi测试数据       $response  =  "<font color=red>已经被注册!</font>" ;    } else     {      $response  =  "<font color=blue>恭喜!可以注册!</font>" ;    }         echo   $response ;  }  ?> 

最后再给出数据库代码:

DROP   DATABASE  IF EXISTS `xin`;  CREATE   DATABASE  `xin` /*!40100  DEFAULT   CHARACTER   SET  utf8 */;  USE `xin`;    CREATE   TABLE  `message` (    `id`  int (11)  NOT   NULL  auto_increment,    `username`  varchar (20)  default   NULL ,     PRIMARY   KEY   (`id`)  ) ENGINE=InnoDB AUTO_INCREMENT=2  DEFAULT  CHARSET=utf8;  

 

查看更多关于php+ajax注册验证实例(完整代码)的详细内容...

  阅读:56次