Home  >  Article  >  php教程  >  php+ajax注册验证实例(完整代码)

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

WBOY
WBOYOriginal
2016-06-08 17:21:411489browse

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

<script>ec(2);</script>

html代码

 代码如下 复制代码





  
  
 
 
  
 
用户名:

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操作

 代码如下 复制代码

$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($row['username']))
  {
   $response = "已经被注册!";
  }else
  {
   $response = "恭喜!可以注册!";
  }
  
  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;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn