Home > Article > Backend Development > PHP+Ajax detects whether a username or email already exists when registering. Ajax tutorial_PHP 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>
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?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; } } }
3. PHP verification page
chkfor.php page code is as follows:
<?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 "用户名可以使用"; } } ?>
I hope the examples described in this article will be helpful to everyone in PHP program development.
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:
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
ebc2d70b107fa53bfcc2bade8c1a70df
b2386ffb911b14667cb8f0f91ea547a7Untitled Document6e916e0f7d1e588d4f442bf645aedb2f
7d54a7c199551efe6681bf7c1d5245a82cacc6d41bbb37262a98f745aa00fbf0
7585b8dcd48cf6e196d35e13f3ce39f5
function check(){ //When the user name is empty
if(document.reg.username.value==""){
document.getElementById('check').innerHTML=" c858d69dd81008e347cd5f5447f8e393Username cannot be empty!e6e38b3c62e8df885fe2e3986461aa63";
document.reg.username.focus();
return false;
}
if(document.getElementById('check').innerHTML==" c858d69dd81008e347cd5f5447f8e393The number is registerede6e38b3c62e8df885fe2e3986461aa63"){ //When the username has been registered (c858d69dd81008e347cd5f5447f8e393The number is registerede6e38b3c62e8df885fe2e3986461aa63 is returned by AJAX)
document.reg.username.focus ();
return false;
}
if(document.reg.userpwd.value==""){ //When the password is empty
document.getElementById('pwd'). innerHTML=" c858d69dd81008e347cd5f5447f8e393User password cannot be empty! e6e38b3c62e8df885fe2e3986461aa63";
document.reg.userpwd.focus();
return false;
}
if(document.reg.userpwd.value.length<6){ // When the password length is wrong
document.getElementById('pwd').innerHTML=" c858d69dd81008e347cd5f5447f8e393The password length cannot be less than 6 characters! e6e38b3c62e8df885fe2e3986461aa63";
document.reg.userpwd.focus();
return false;
}
if (document.reg.reuser...the rest Full text>>
7c6ecc2a75ade4ffa1f50134f6be3b0e
var username=$('#username').val();
$.post('xx.php',{username:username},function (data){
if(data==1){
alert("Already exists");//Or let a span or div display $('#xx').html("Already exists") ;
}
});
2cacc6d41bbb37262a98f745aa00fbf0
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);