Home > Backend Development > PHP Tutorial > PHP+Ajax detects whether a username or email already exists when registering. Ajax tutorial_PHP tutorial

PHP+Ajax detects whether a username or email already exists when registering. Ajax tutorial_PHP tutorial

WBOY
Release: 2016-07-13 10:20:18
Original
1221 people have browsed it

PHP+Ajax detects whether an example tutorial already exists when registering by user name or email, ajax example tutorial

PHP+Ajax detecting whether the username or email already exists when registering is a common and important function in forums or membership systems. This article briefly describes the implementation method of this function in the form of examples. The specific steps are as follows:

1. PHP detection page

The check.php page code is as follows:

<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

2. Ajax verification page

The check.js page code is as follows:

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

3. PHP verification page

chkfor.php page code is as follows:

<&#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

I hope the examples described in this article will be helpful to everyone in PHP program development.

That friend has PHP+AJAX code to verify username

Files include:

userreg.html (registration page)
ajaxreg .js (AJAX script and real-time verification JS script)
checkuserreg.php (connect to the database and check whether the user name has been Registration page)
userreg.html (Registration page) Copy PHP content to clipboard
PHP code:



Untitled Document


xx.php
$username=$_POST['username'];
$sql="select * from user where username=$username";
$handle=mysql_query($sql);
$num=mysql_num_rows($handle);
if($num>0){
$flag=1;
}else{
$flag=0;
}
exit($flag);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868224.htmlTechArticlePHP+Ajax detects whether the user name or email already exists when registering, ajax example tutorial PHP+Ajax detects the user Whether the name or email already exists when registering is a common problem in forums or membership systems...
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