PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程
PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下:
一、PHP检测页面
check.php页面代码如下:
<scripttype="text/javascript"src="jiance.js"></script> <formname="myform"action=""method="get"> 用户名:<inputname="user"value=""type="text"onblur="funtest100()"/> <divid="test100"></div> </form>
二、Ajax验证页面
check.js页面代码如下:
varxmlHttp; functionS_xmlhttprequest(){ if(window.ActiveXobject){ xmlHttp=newActiveXObject('Microsoft.XMLHTTP'); }elseif(window.XMLHttpRequest){ xmlHttp=newXMLHttpRequest(); } } functionfuntest100(){ varf=document.getElementsByTagName_r('form')[0].user.value;//获取文本框内容 S_xmlhttprequest(); xmlHttp.open("GET","jcfor.php?id="+f,true);//找开请求 xmlHttp.onreadystatechange=byphp;//准备就绪执行 xmlHttp.send(null);//发送 } functionbyphp(){ //判断状态 if(xmlHttp.readyState==1){//Ajax状态 document.getElementByIdx_x_x('test100').innerHTML="正在加载"; } if(xmlHttp.readyState==4){//Ajax状态 if(xmlHttp.status==200){//服务器端状态 varbytest100=xmlHttp.responseText; //alert(bytest100); document.getElementByIdx_x_x('test100').innerHTML=bytest100; } } }
三、PHP验证页面
chkfor.php页面代码如下:
<?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"用户名可以使用"; } } ?>
希望本文所述实例对大家PHP程序开发有所帮助。