PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程_PHP

WBOY
Release: 2016-05-31 19:30:13
Original
776 people have browsed it

PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下:

一、PHP检测页面

check.php页面代码如下:

<script type="text/javascript" src="jiance.js"></script>
<form name="myform" action="" method="get">
 用户名:<input name="user" value="" type="text" onblur="funtest100()" />
 <div id="test100"></div>
</form>

Copy after login

二、Ajax验证页面

check.js页面代码如下:

var xmlHttp;
function S_xmlhttprequest(){
  if(window.ActiveXobject){
    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  }else if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest();
  }
}
function funtest100(){
  var f = document.getElementsByTagName_r('form')[0].user.value;//获取文本框内容
  S_xmlhttprequest();
  xmlHttp.open("GET","jcfor.php&#63;id="+f,true);//找开请求
  xmlHttp.onreadystatechange = byphp;//准备就绪执行
  xmlHttp.send(null);//发送
}
function byphp(){
  //判断状态
  if(xmlHttp.readyState==1){//Ajax状态
    document.getElementByIdx_x_x('test100').innerHTML = "正在加载";
  }
  if(xmlHttp.readyState==4){//Ajax状态
    if(xmlHttp.status==200){//服务器端状态
      var bytest100 = xmlHttp.responseText;
      //alert(bytest100);
      document.getElementByIdx_x_x('test100').innerHTML = bytest100; 
    }  
  }
}

Copy after login

三、PHP验证页面

chkfor.php页面代码如下:

<&#63;php
 if($_GET[id]){
    sleep(1);
    $conn=mysql_connect('localhost','root','');
    mysql_select_db('test',$conn);
    $sql="SELECT * FROM `user` WHERE `name`='$_GET[id]'";
    $q=mysql_query($sql);
 
    if(is_array(mysql_fetch_row($q))){
      echo "用户名已经存在"; 
    }else{
      echo "用户名可以使用"; 
    }
 }  
&#63;>

Copy after login

希望本文所述实例对大家PHP程序开发有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!